-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (60 loc) · 2.15 KB
/
Main.java
File metadata and controls
71 lines (60 loc) · 2.15 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
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import javafx.scene.layout.Priority;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
HashMap<Integer, ArrayList<Integer>> rule = new HashMap<Integer, ArrayList<Integer>>();
int[] inDegree = new int[N+1]; // 진입차수
Arrays.fill(inDegree, 0);
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
int preceding = Integer.parseInt(st.nextToken());
int curr = Integer.parseInt(st.nextToken());
ArrayList<Integer> arr;
if(rule.get(preceding) == null) {
arr = new ArrayList<Integer>();
arr.add(curr);
rule.put(preceding, arr);
} else {
arr = rule.get(preceding);
arr.add(curr);
rule.put(preceding, arr);
}
inDegree[curr]++;
}
// 위상정렬 사용. 진입차수 0인 노드들을 Q에 넣는다.
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for(int i=1; i<N+1; i++){
if(inDegree[i] == 0) {
q.offer(i);
}
}
ArrayList<Integer> result = new ArrayList<Integer>();
while(!q.isEmpty()){
int data = q.poll();
result.add(data);
if(rule.get(data) != null){
for(int book : rule.get(data)){
inDegree[book]--;
if(inDegree[book] == 0){
q.add(book);
}
}
}
}
for(int book : result){
System.out.print(book+" ");
}
br.close();
}
}