-
Notifications
You must be signed in to change notification settings - Fork 0
/
BridgesInAGraph.java
79 lines (48 loc) · 1.63 KB
/
BridgesInAGraph.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
70
71
72
73
74
75
76
77
78
79
class Solution {
static int times;
public static void dfs(int u ,int p,List<List<Integer>> adj, List<List<Integer>> al , int low[] , int[] time , boolean vis[])
{
vis[u]=true;
low[u]=time[u]=times;
times++;
for(int v: adj.get(u) )
{
if(v==p) continue;
if(vis[v]==true)
{
low[u]=Math.min(low[u],low[v]);
}else{
dfs(v,u,adj,al,low,time,vis);
low[u]=Math.min(low[u],low[v]);
if(low[v]> time[u])
{
List<Integer> tmp= new ArrayList<>();
tmp.add(u);
tmp.add(v);
al.add(tmp);
}
}
}
}
public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
int times=1;
int low[]= new int[n];
int time[]= new int[n];
boolean vis[]= new boolean[n];
List<List<Integer>> lst = new ArrayList<>();
for(int i=0;i<n;i++)
{
lst.add(new ArrayList<Integer>());
}
for(int i=0;i<connections.size();i++)
{
int aa=connections.get(i).get(0);
int bb=connections.get(i).get(1);
lst.get(aa).add(bb);
lst.get(bb).add(aa);
}
List<List<Integer>> al = new ArrayList<>();
dfs(0,0,lst , al , low , time, vis);
return al;
}
}