-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
63 lines (47 loc) · 1.61 KB
/
Main.java
File metadata and controls
63 lines (47 loc) · 1.61 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static Queue<Integer> result = new LinkedList<>();
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());
ArrayList<Integer>[] graph = new ArrayList[N + 1];
for (int i = 0; i < N + 1; i++) {
graph[i] = new ArrayList<>();
}
int[] link = new int[N + 1];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
graph[A].add(B);
link[B] += 1;
}
topology_sort(N, link, graph);
while(!result.isEmpty()) {
System.out.print(result.poll() + " ");
}
}
public static void topology_sort(int n, int[] link, ArrayList<Integer>[] graph) {
Queue<Integer> q = new LinkedList<>();
for (int i = 1; i < n + 1; i++) {
if (link[i] == 0) {
q.offer(i);
}
}
while (!q.isEmpty()) {
int now = q.poll();
result.offer(now);
for (int a : graph[now]) {
link[a] -= 1;
if (link[a] == 0) {
q.offer(a);
}
}
}
}
}