Repository navigation
Expand file tree
/
Copy pathCode03_TopoSortStaticLuogu.java
More file actions
152 lines (129 loc) · 3.53 KB
/
Copy pathCode03_TopoSortStaticLuogu.java
File metadata and controls
152 lines (129 loc) · 3.53 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package class059;
// 字典序最小的拓扑排序
// 要求返回所有正确的拓扑排序中 字典序最小 的结果
// 建图请使用链式前向星方式,因为比赛平台用其他建图方式会卡空间
// 测试链接 : https://www.luogu.com.cn/problem/U107394
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下所有代码,把主类名改成Main,可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Code03_TopoSortStaticLuogu {
public static int MAXN = 100001;
public static int MAXM = 100001;
// 建图相关,链式前向星
public static int[] head = new int[MAXN];
public static int[] next = new int[MAXM];
public static int[] to = new int[MAXM];
public static int cnt;
// 拓扑排序,不用队列,用小根堆,为了得到字典序最小的拓扑排序
public static int[] heap = new int[MAXN];
public static int heapSize;
// 拓扑排序,入度表
public static int[] indegree = new int[MAXN];
// 收集拓扑排序的结果
public static int[] ans = new int[MAXN];
public static int n, m;
// 清理之前的脏数据
public static void build(int n) {
cnt = 1;
heapSize = 0;
Arrays.fill(head, 0, n + 1, 0);
Arrays.fill(indegree, 0, n + 1, 0);
}
// 用链式前向星建图
public static void addEdge(int f, int t) {
next[cnt] = head[f];
to[cnt] = t;
head[f] = cnt++;
}
// 小根堆里加入数字
public static void push(int num) {
int i = heapSize++;
heap[i] = num;
// heapInsert的过程
while (heap[i] < heap[(i - 1) / 2]) {
swap(i, (i - 1) / 2);
i = (i - 1) / 2;
}
}
// 小根堆里弹出最小值
public static int pop() {
int ans = heap[0];
heap[0] = heap[--heapSize];
// heapify的过程
int i = 0;
int l = 1;
while (l < heapSize) {
int best = l + 1 < heapSize && heap[l + 1] < heap[l] ? l + 1 : l;
best = heap[best] < heap[i] ? best : i;
if (best == i) {
break;
}
swap(best, i);
i = best;
l = i * 2 + 1;
}
return ans;
}
// 判断小根堆是否为空
public static boolean isEmpty() {
return heapSize == 0;
}
// 交换堆上两个位置的数字
public static void swap(int i, int j) {
int tmp = heap[i];
heap[i] = heap[j];
heap[j] = tmp;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
n = (int) in.nval;
in.nextToken();
m = (int) in.nval;
build(n);
for (int i = 0, from, to; i < m; i++) {
in.nextToken();
from = (int) in.nval;
in.nextToken();
to = (int) in.nval;
addEdge(from, to);
indegree[to]++;
}
topoSort();
for (int i = 0; i < n - 1; i++) {
out.print(ans[i] + " ");
}
out.println(ans[n - 1]);
}
out.flush();
out.close();
br.close();
}
public static void topoSort() {
for (int i = 1; i <= n; i++) {
if (indegree[i] == 0) {
push(i);
}
}
int fill = 0;
while (!isEmpty()) {
int cur = pop();
ans[fill++] = cur;
// 用链式前向星的方式,遍历cur的相邻节点
for (int ei = head[cur]; ei != 0; ei = next[ei]) {
if (--indegree[to[ei]] == 0) {
push(to[ei]);
}
}
}
}
}