-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCriticalConnectionsInANetwork.java
50 lines (47 loc) · 1.6 KB
/
CriticalConnectionsInANetwork.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
/*https://leetcode.com/problems/critical-connections-in-a-network/*/
class Solution {
int timer;
int[] disc, low, parent;
boolean[] visited;
List<List<Integer>> result;
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
result = new ArrayList<List<Integer>>();
visited = new boolean[n];
disc = new int[n];
low = new int[n];
parent = new int[n];
Arrays.fill(parent,-1);
timer = 0;
int i;
for (i = 0; i < n; ++i)
graph.add(new ArrayList<Integer>());
for (List<Integer> edge : connections)
{
graph.get(edge.get(0)).add(edge.get(1));
graph.get(edge.get(1)).add(edge.get(0));
}
for (i = 0; i < n; ++i)
if (!visited[i])
dfs(graph,i);
return result;
}
private void dfs(ArrayList<ArrayList<Integer>> graph, int src)
{
visited[src] = true;
disc[src] = low[src] = ++timer;
for (int adjNode : graph.get(src))
{
if (!visited[adjNode]) //tree edge
{
parent[adjNode] = src;
dfs(graph,adjNode);
low[src] = Math.min(low[src],low[adjNode]);
}
else if (parent[src] != adjNode) //back edge
low[src] = Math.min(low[src],disc[adjNode]);
if (low[adjNode] > disc[src]) //bridge rule
result.add(Arrays.asList(src,adjNode));
}
}
}