Skip to content

Commit

Permalink
Create 1192.Critical-Connections-in-a-Network.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Sep 18, 2019
1 parent 93c5761 commit a3df9c2
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Solution {
vector<vector<int>>rets;
vector<vector<int>>next;
int time = 0;
public:
vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections)
{
next.resize(n);
for (auto x:connections)
{
next[x[0]].push_back(x[1]);
next[x[1]].push_back(x[0]);
}

vector<bool>visited(n);
vector<int>dfn(n,0);
vector<int>low(n,0);
vector<int>parent(n,-1);

for (int i = 0; i < n; i++)
{
if (visited[i] == false)
DFS(i, visited, dfn, low, parent);
}

return rets;
}


void DFS(int u, vector<bool>&visited, vector<int>&dfn, vector<int>&low, vector<int>&parent)
{
visited[u] = true;

dfn[u] = low[u] = ++time;

for (int v : next[u])
{
if (v == parent[u])
continue;

if (!visited[v])
{
parent[v] = u;
DFS(v, visited, dfn, low, parent);

if (low[v] > dfn[u]) rets.push_back({v,u}); // critical edge

// if(low[v] >= dfn[u]), then u is a critical vertex

low[u] = min(low[u], low[v]);
}
else
{
low[u] = min(low[u], dfn[v]);
}
}
}

};

0 comments on commit a3df9c2

Please sign in to comment.