UVA OJ - 796, Bridge finding

parent 0d52e0ee
#include <cstdio>
#include <algorithm>
#include <vector>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair< int, int > pii;
int N, cnt = 0;
vector< vector< int > > G;
vector< int > low, time, visited;
vector< pii > bridges;
void dfs( int u, int p ) {
time[ u ] = ++cnt;
low[ u ] = time[ u ];
for( int i = 0; i < G[ u ].size(); i++ ) {
int v = G[ u ][ i ];
if( v == p ) continue;
if( !time[ v ] ) {
dfs( v, u );
if( time[ u ] < low[ v ] ) bridges.pb( mp( min( u, v ), max( u, v ) ) );
low[ u ] = min( low[ u ], low[ v ] );
} else {
low[ u ] = min( low[ u ], time[ v ] );
}
}
}
int main( void ) {
while( scanf("%d", &N ) == 1 ) {
G.resize( N + 1 );
low.resize( N + 1, 0 );
time.resize( N + 1, 0 );
for( int i = 0; i < N; i++ ) {
int u, e, v;
scanf("%d (%d)", &u, &e );
for( int j = 0; j < e; j++ ) {
scanf("%d", &v );
G[ u ].pb( v );
}
}
for( int i = 0; i < N; i++ ) {
if( !time[ i ] ) dfs( i, -1 );
}
sort( bridges.begin(), bridges.end() );
printf("%d critical links\n", bridges.size() );
for( int i = 0; i < bridges.size(); i++ ) {
printf("%d - %d\n", bridges[ i ].X, bridges[ i ].Y );
}
printf("\n");
cnt = 1;
bridges.clear();
for( int i = 0; i < N; i++ ) G[ i ].clear();
low.clear();
time.clear();
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment