-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ2252.java
More file actions
54 lines (41 loc) · 1.42 KB
/
J2252.java
File metadata and controls
54 lines (41 loc) · 1.42 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
public class J2252 {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int M = Integer.parseInt(input[1]);
LinkedList<Integer> list[] = new LinkedList[N+1];
for (int i = 1; i <= N; i++) {
list[i] = new LinkedList<>();
}
int d[] = new int[N+1];
for (int i = 0; i < M; i++) {
input = buffer.readLine().split(" ");
int start = Integer.parseInt(input[0]);
int end = Integer.parseInt(input[1]);
list[start].add(end);
d[end]++;
}
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i = 1; i <=N ; i++) {
if(d[i] == 0) queue.offer(i);
}
int key = 0;
while(!queue.isEmpty()){
int size = queue.size();
for (int i = 0; i < size; i++) {
key = queue.poll();
for(int k : list[key]){
d[k]--;
if(d[k] == 0) queue.offer(k);
}
System.out.print(key + " ");
}
}
}
}