-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1192-critical-connections-in-a-network.js
59 lines (53 loc) · 1.8 KB
/
1192-critical-connections-in-a-network.js
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
/**
* 1192. Critical Connections in a Network
* https://leetcode.com/problems/critical-connections-in-a-network/
* Difficulty: Hard
*
* There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections
* forming a network where connections[i] = [ai, bi] represents a connection between servers ai and
* bi. Any server can reach other servers directly or indirectly through the network.
*
* A critical connection is a connection that, if removed, will make some servers unable to reach
* some other server.
*
* Return all critical connections in the network in any order.
*/
/**
* @param {number} n
* @param {number[][]} connections
* @return {number[][]}
*/
var criticalConnections = function(n, connections) {
const graph = Array.from({ length: n }, () => []);
const discoveryTimes = new Array(n).fill(-1);
const lowestReachableTimes = new Array(n).fill(-1);
const criticalEdges = [];
let time = 0;
connections.forEach(([from, to]) => {
graph[from].push(to);
graph[to].push(from);
});
exploreNode(0, -1);
return criticalEdges;
function exploreNode(current, parent) {
discoveryTimes[current] = lowestReachableTimes[current] = time++;
for (const neighbor of graph[current]) {
if (neighbor === parent) continue;
if (discoveryTimes[neighbor] === -1) {
exploreNode(neighbor, current);
lowestReachableTimes[current] = Math.min(
lowestReachableTimes[current],
lowestReachableTimes[neighbor]
);
if (lowestReachableTimes[neighbor] > discoveryTimes[current]) {
criticalEdges.push([current, neighbor]);
}
} else {
lowestReachableTimes[current] = Math.min(
lowestReachableTimes[current],
discoveryTimes[neighbor]
);
}
}
}
};