-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCritical Connections in a Network.cpp
60 lines (44 loc) · 1.6 KB
/
Critical Connections in a Network.cpp
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
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
// vector<int> p;
vector<vector<int>> adj;
vector<int> low;
vector<int> dis;
vector<vector<int>> ans;
int t = 1;
void dfs(int u, int p){
dis[u] = low[u] = t++;
for (int next : adj[u]) {
if (dis[next] == 0) {
dfs(next, u);
low[u] = min(low[u], low[next]);
} else if (next != p)
low[u] = min(low[u], dis[next]);
if (low[next] > dis[u])
ans.push_back({u, next});
}
}
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
// p.resize(n,-1);
adj.resize(n);
low.resize(n,0);
dis.resize(n,0);
for(auto x: connections){
adj[x[0]].push_back(x[1]);
adj[x[1]].push_back(x[0]);
}
dfs(0,-1);
return ans;
}
};