-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_2252.java
More file actions
57 lines (44 loc) · 1.49 KB
/
baekjoon_2252.java
File metadata and controls
57 lines (44 loc) · 1.49 KB
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
import java.util.*;
import java.io.*;
public class baekjoon_2252 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] w = new int[N + 1];
List<List<Integer>> graph = new ArrayList<>();
for(int i=0;i<=N;i++) {
graph.add(new ArrayList<>());
}
for(int i=1;i<=M;i++) {
st= new StringTokenizer(br.readLine());
int front = Integer.parseInt(st.nextToken());
int back = Integer.parseInt(st.nextToken());
w[back]++;
graph.get(front).add(back);
}
Queue<Integer> queue = new ArrayDeque<>();
for(int i=1;i<=N;i++){
if(w[i] == 0) {
queue.add(i);
}
}
StringBuilder sb = new StringBuilder();
while(!queue.isEmpty()) {
int cur = queue.poll();
sb.append(cur + " ");
for(Integer n : graph.get(cur)) {
w[n]--;
if(w[n] == 0) {
queue.add(n);
}
}
}
sb.append("\n");
bw.write(sb.toString());
bw.flush();
bw.close();
}
}