[Link] https://www.acmicpc.net/problem/3977
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
static int sccIdx = 1, groupIdx = 0;
static Stack<Integer> stack = new Stack<>();
static int[] parent;
static int[] group;
static boolean[] finished;
static Node[] nodes;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
ArrayList<Edge> edges = new ArrayList<>();
int test = toi(br.readLine());
int[] arr;
for(int iter = 0; iter < test; iter++) {
arr = getArr();
int n = arr[0], m = arr[1];
parent = new int[n];
group = new int[n];
Arrays.fill(parent, -1);
edges.clear();
finished = new boolean[n];
nodes = new Node[n];
for(int i = 0; i < n; i++) nodes[i] = new Node(i);
for(int i = 0; i < m; i++) {
arr = getArr();
int a = arr[0], b = arr[1];
nodes[a].list.add(b);
edges.add(new Edge(a, b));
}
sccIdx = 1;
groupIdx = 0;
for(int i = 0; i < n; i++) {
if(parent[i] == -1) scc(i);
}
int[] deg = new int[groupIdx];
for(Edge edge: edges) {
int a = edge.from, b = edge.to;
if(group[a] == group[b]) continue;
deg[group[b]]++;
}
int degZeroCnt = 0, degZeroIdx = 0;
for(int i = 0; i < groupIdx; i++) {
if(deg[i] == 0) {
degZeroCnt++;
degZeroIdx = i;
}
}
if(degZeroCnt != 1) sb.append("Confused\n");
else {
ArrayList<Integer> tmp = new ArrayList<>();
for(int i = 0; i < n; i++)
if(group[i] == degZeroIdx) tmp.add(i);
Collections.sort(tmp);
for(int e: tmp) sb.append(e).append("\n");
}
sb.append("\n");
if(iter != test - 1) br.readLine();
}
print(sb);
}
static int scc(int idx) {
int origVal = parent[idx] = sccIdx++;
stack.push(idx);
for(int v: nodes[idx].list) {
if(parent[v] == -1) parent[idx] = Math.min(parent[idx], scc(v));
else if(finished[v] == false) parent[idx] = Math.min(parent[idx], parent[v]);
}
if(parent[idx] == origVal) {
while(true) {
int pop = stack.pop();
finished[pop] = true;
group[pop] = groupIdx;
if(pop == idx) break;
}
groupIdx++;
}
return parent[idx];
}
static class Node {
int idx;
ArrayList<Integer> list = new ArrayList<>();
public Node(int idx) { this.idx = idx; }
}
static class Edge {
int from, to;
public Edge(int from, int to) { this.from = from; this.to = to; }
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}