Skip to content

Commit 16814f6

Browse files
authored
Create 1192-critical-connections-in-a-network.js
1 parent 902743e commit 16814f6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} connections
4+
* @return {number[][]}
5+
*/
6+
const criticalConnections = function(n, connections) {
7+
const g = [],
8+
low = Array(n),
9+
res = []
10+
low.fill(0)
11+
for (let con of connections) {
12+
g[con[0]] = g[con[0]] || []
13+
g[con[1]] = g[con[1]] || []
14+
g[con[0]].push(con[1])
15+
g[con[1]].push(con[0])
16+
}
17+
const dfs = function(cur, v, p) {
18+
let dfn = cur
19+
low[v] = cur
20+
for (let i of g[v]) {
21+
if (i != p) {
22+
if (low[i] == 0) {
23+
cur++
24+
dfs(cur, i, v)
25+
if (low[i] > dfn) {
26+
res.push([i, v])
27+
}
28+
}
29+
low[v] = Math.min(low[v], low[i])
30+
}
31+
}
32+
}
33+
dfs(1, 0, -1)
34+
return res
35+
}

0 commit comments

Comments
 (0)