-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution802.java
69 lines (57 loc) · 1.93 KB
/
Solution802.java
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
package leetcode.graph;
import java.util.*;
public class Solution802 {
public List<Integer> eventualSafeNodes(int[][] graph) {
Graph g = new Graph(graph);
return g.topological();
}
static class Graph {
private int V;
private LinkedList<Integer>[] adj;
// 入度
private int[] inDegree;
public Graph(int[][] graph) {
this.V = graph.length;
inDegree = new int[V];
adj = new LinkedList[V];
for (int i = 0; i < adj.length; i++) {
adj[i] = new LinkedList<>();
}
// 构造反向图
for (int i = 0; i < graph.length; i++) {
for (int v : graph[i]) {
adj[v].add(i);
}
// 统计入度
inDegree[i] = graph[i].length;
}
}
// 拓扑排序是找到图中入度为 0 的节点,以及由入度为 0 节点所指向的节点
public List<Integer> topological() {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// 入度为 0,则表示该节点为终端节点
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] == 0) {
deque.addLast(i);
}
}
while (!deque.isEmpty()) {
Integer poll = deque.poll();
for (Integer w : adj[poll]) {
// 将以其为起点的有向边删除,更新终点入度
inDegree[w]--;
if (inDegree[w] == 0) {
deque.addLast(w);
}
}
}
List<Integer> res = new ArrayList<>();
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] == 0) {
res.add(i);
}
}
return res;
}
}
}