-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2252.java
More file actions
61 lines (51 loc) · 1.87 KB
/
2252.java
File metadata and controls
61 lines (51 loc) · 1.87 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
/**
* 백준 2252번 줄 세우기
* 위상 정렬
* 진입 차수(indegree) : 들어오는 간선의 개수
* 진출 차수(outdegree) : 나가는 간선의 개수
*/
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
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());
Deque<Integer> queue = new ArrayDeque<>();
int indegree[] = new int[n+1]; // 진입 차수 배열
List<Integer> result = new ArrayList<>(); // 결과 리스트
List<List<Integer>> graph = new ArrayList<>();
for(int i=0; i<=n; i++){
graph.add(new ArrayList<>());
}
for(int i=0; i<m; i++){
st = new StringTokenizer(br.readLine()," ");
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
graph.get(v).add(w); // v -> w
indegree[w]++; // w 진입 차수 증가
}
// 위상 정렬
// 진입 차수가 0인 정점 먼저 추가
for(int i=1; i<=n; i++){
if(indegree[i]==0){
queue.offer(i);
}
}
while(!queue.isEmpty()){
int node = queue.pop();
result.add(node); // 진입 차수 0일 때 추가
// node가 출발 정점인 간선 삭제
for(int i=0; i<graph.get(node).size(); i++){
int w = graph.get(node).get(i);
indegree[w]--;
if(indegree[w]==0)
queue.offer(w);
}
}
for(Integer student : result){
System.out.print(student+" ");
}
}
}