https://www.acmicpc.net/problem/1889
위상 정렬
방향 그래프
import java.io.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
// input
private static BufferedReader br;
// variables
private static int N;
private static int[] degrees;
private static List<List<Integer>> adjList;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
input();
bw.write(solve());
bw.close();
} // End of main()
private static String solve() {
StringBuilder sb = new StringBuilder();
BFS();
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= N; i++) {
if (degrees[i] == 2) {
list.add(i);
}
}
if (list.isEmpty()) {
sb.append(0);
} else {
sb.append(list.size()).append('\n');
for (int num : list) sb.append(num).append(' ');
}
return sb.toString();
} // End of solve()
private static void BFS() {
ArrayDeque<Integer> que = new ArrayDeque<>();
boolean[] isVisited = new boolean[N + 1];
for (int i = 1; i <= N; i++) {
if (degrees[i] < 2) {
que.offer(i);
}
}
while (!que.isEmpty()) {
int cur = que.poll();
if (isVisited[cur]) continue;
isVisited[cur] = true;
for (int next : adjList.get(cur)) {
degrees[next]--;
if (degrees[next] < 2) {
que.offer(next);
}
}
}
} // End of BFS()
private static void input() throws IOException {
N = Integer.parseInt(br.readLine());
adjList = new ArrayList<>();
degrees = new int[N + 1];
for (int i = 0; i <= N; i++) {
adjList.add(new ArrayList<>());
}
for (int i = 1; i <= N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < 2; j++) {
int temp = Integer.parseInt(st.nextToken());
adjList.get(i).add(temp);
degrees[temp]++;
}
}
} // End of input()
} // End of Main class