public static void main(String[] args) { // TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int N = scan.nextInt(); // 간선 갯수
int M = scan.nextInt();
int line[] = new int[N + 1];
@SuppressWarnings("unchecked") ArrayList<Integer> arrayList[] = new ArrayList[N + 1]; for (int i = 0; i < N + 1; i++) { arrayList[i] = new ArrayList<Integer>(); }
for (int i = 0; i < M; i++) {
int num = scan.nextInt();
if (num == 0) continue;
int prev, cur;
prev = scan.nextInt();
for (int j = 0; j < num - 1; j++) {
cur = scan.nextInt();
line[cur]++;
arrayList[prev].add(cur);
prev = cur;
}
}
LinkedList<Integer> queue = new LinkedList();
for (int i = 1; i <= N; i++) {
if (line[i] == 0) { queue.add(i); } }
int result[] = new int[N + 1];
// 큐에 집어 넣는 과정을 이곳에서 해야 함... for (int i = 1; i <= N; i++) {
if (queue.isEmpty()) { System.out.println(0); break; }
int poped = (int) queue.poll();
result[i] = poped;
for (int j = 0; j < arrayList[poped].size(); j++) { int next = arrayList[poped].get(j);
pumpyboom 9년 전
테스트케이스도 다 맞고...
오류도 없다고 생각합니다.
시스템상 문제가 있는 부분이 있는것 같습니다. 어느부분이 잘못된 것일까요?
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int N = scan.nextInt(); // 간선 갯수
int M = scan.nextInt();
int line[] = new int[N + 1];
@SuppressWarnings("unchecked")
ArrayList<Integer> arrayList[] = new ArrayList[N + 1];
for (int i = 0; i < N + 1; i++) {
arrayList[i] = new ArrayList<Integer>();
}
for (int i = 0; i < M; i++) {
int num = scan.nextInt();
if (num == 0)
continue;
int prev, cur;
prev = scan.nextInt();
for (int j = 0; j < num - 1; j++) {
cur = scan.nextInt();
line[cur]++;
arrayList[prev].add(cur);
prev = cur;
}
}
LinkedList<Integer> queue = new LinkedList();
for (int i = 1; i <= N; i++) {
if (line[i] == 0) {
queue.add(i);
}
}
int result[] = new int[N + 1];
// 큐에 집어 넣는 과정을 이곳에서 해야 함...
for (int i = 1; i <= N; i++) {
if (queue.isEmpty()) {
System.out.println(0);
break;
}
int poped = (int) queue.poll();
result[i] = poped;
for (int j = 0; j < arrayList[poped].size(); j++) {
int next = arrayList[poped].get(j);
if (--line[next] == 0) {
queue.add(next);
}
}
System.out.println(result[i]);
}
}
}