Created
August 31, 2019 17:36
-
-
Save limabeans/89c1b017d1d000ffb8be5e00cad41d8c to your computer and use it in GitHub Desktop.
uva 796, sol to find bridges in graph problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://onlinejudge.org/external/7/796.pdf | |
#include <bits/stdc++.h> | |
using namespace std; | |
typedef long long ll; | |
template<typename T> | |
void out(T x) { cout << x << endl; exit(0); } | |
#define watch(x) cout << (#x) << " is " << (x) << endl | |
int n; | |
vector<set<int>> g; | |
vector<int> low, tin, viz; | |
int cloc = 0; | |
void dfs(int at, int par) { | |
viz[at] = 1; | |
tin[at] = cloc++; | |
low[at] = tin[at]; | |
for (int to: g[at]) { | |
if (to == par) continue; | |
if (viz[to]) { | |
low[at] = min(low[at], tin[to]); | |
} else { | |
dfs(to, at); | |
low[at] = min(low[at], low[to]); | |
} | |
} | |
} | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); | |
while (scanf("%d", &n) == 1) { | |
g = {}; | |
g.resize(n); | |
for (int i=0; i<n; i++) { | |
int u; int d; int v; | |
scanf("%d ", &u); | |
scanf("(%d)", &d); | |
while (d--) { | |
scanf("%d", &v); | |
g[u].insert(v); | |
g[v].insert(u); | |
} | |
} | |
low = {}; tin = {}; viz = {}; | |
low.resize(n); tin.resize(n); viz.resize(n); | |
cloc = 0; | |
for (int i=0; i<n; i++) { | |
if (viz[i] == 0) { | |
dfs(i,-1); | |
} | |
} | |
// for (int i=0; i<n; i++) { | |
// cout<<i<<" tin: "<<tin[i]<<" low: "<<low[i]<<endl; | |
// } | |
set<pair<int,int>> bridges; | |
for (int i=0; i<n; i++) { | |
for (int j=0; j<n; j++) { | |
if (g[i].count(j) == 0 || g[j].count(i) == 0) continue; | |
if (low[j] > tin[i]) { | |
int x = min(i,j); int y = max(i,j); | |
bridges.insert({x,y}); | |
} | |
} | |
} | |
printf("%d critical links\n", (int)bridges.size()); | |
for (auto ed: bridges) { | |
printf("%d - %d\n", ed.first, ed.second); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment