forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1192.go
39 lines (36 loc) · 786 Bytes
/
1192.go
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
var g [][]int
var low []int
var res [][]int
func criticalConnections(n int, connections [][]int) [][]int {
g = make([][]int, n)
low = make([]int, n)
res = make([][]int, 0)
for _, e := range connections {
g[e[0]] = append(g[e[0]], e[1])
g[e[1]] = append(g[e[1]], e[0])
}
dfs(1, 0, -1)
return res
}
func dfs(cur, v, p int) {
dfn := cur
low[v] = cur
for _, i := range g[v] {
if i != p {
if low[i] == 0 {
cur++
dfs(cur, i, v)
if low[i] > dfn {
res = append(res, []int{i, v})
}
}
low[v] = min(low[v], low[i])
}
}
}
func min(x, y int) int {
if x < y {
return x
}
return y
}