-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1766.java
More file actions
58 lines (46 loc) · 1.47 KB
/
J1766.java
File metadata and controls
58 lines (46 loc) · 1.47 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
58
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class J1766 {
static int n, m, countArr[];
static ArrayList<Integer>[] list;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
list = new ArrayList[n+1];
countArr = new int[n+1];
for(int i = 0; i <=n; i++){
list[i] = new ArrayList<>();
}
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);
countArr[end]++;
}
polo();
}
public static void polo(){
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 1; i <= n; i++){
if(countArr[i] ==0){
pq.offer(i);
}
}
while(!pq.isEmpty()){
int cur = pq.poll();
System.out.print(cur+ " ");
for(int now : list[cur]){
countArr[now]--;
if(countArr[now] ==0){
pq.offer(now);
}
}
}
}
}