- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1766.java
More file actions
Latest commit
58 lines (46 loc) · 1.47 KB
/
Copy pathJ1766.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
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.PriorityQueue;
publicclassJ1766 {
staticintn, m, countArr[];
staticArrayList<Integer>[] list;
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderbuffer = newBufferedReader(newInputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
list = newArrayList[n+1];
countArr = newint[n+1];
for(inti = 0; i <=n; i++){
list[i] = newArrayList<>();
}
for(inti = 0; i< m; i++){
input = buffer.readLine().split(" ");
intstart = Integer.parseInt(input[0]);
intend = Integer.parseInt(input[1]);
list[start].add(end);
countArr[end]++;
}
polo();
}
publicstaticvoidpolo(){
PriorityQueue<Integer> pq = newPriorityQueue<>();
for(inti = 1; i <= n; i++){
if(countArr[i] ==0){
pq.offer(i);
}
}
while(!pq.isEmpty()){
intcur = pq.poll();
System.out.print(cur+ " ");
for(intnow : list[cur]){
countArr[now]--;
if(countArr[now] ==0){
pq.offer(now);
}
}
}
}
}