Cod sursa(job #616180)

Utilizator BitOneSAlexandru BitOne Data 11 octombrie 2011 21:28:08
Problema Componente biconexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011

using namespace std;
int idx, countBCC;
int lowLink[N_MAX], dfsIdx[N_MAX];
vector< int > G[N_MAX], BCC[N_MAX];
vector< pair< int, int > > S;
inline int _min( int x, int y ) { return x <= y ? x : y; }
inline void DFS( int x )
{
	lowLink[x]=dfsIdx[x]=++idx;
	vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
	for( ; it< iend; ++it )
	{
		if( 0 == dfsIdx[*it] )
		{
			S.push_back( pair< int, int >( x, *it ) );
			DFS( *it );
			lowLink[x]=_min( lowLink[x], lowLink[*it] );
			if( lowLink[*it] >= dfsIdx[x] )
			{
				pair< int, int > now( x, *it ), y;
				do
				{
					y=S.back(); S.pop_back();
					BCC[countBCC].push_back(y.first);
					BCC[countBCC].push_back(y.second);
				}while( y != now );
				++countBCC;
			}
		}
		else lowLink[x]=_min( lowLink[x], dfsIdx[*it] );
	}
}
int main( void )
{
	int N, M, x, y, i;
	ifstream in( "biconex.in" );
	for( in>>N>>M; M; --M )
	{
		in>>x>>y;
		G[x].push_back( y );
		G[y].push_back( x );
	}
	for( i=1; i <= N; ++i )
		if( 0 == dfsIdx[i] )
			DFS(i);
	ofstream out( "biconex.out" );
	out<<countBCC<<'\n';
	for( i=0; i < countBCC; ++i )
	{
		sort( BCC[i].begin(), BCC[i].end() );
		vector< int >::iterator iend=unique( BCC[i].begin(), BCC[i].end() );
		copy( BCC[i].begin(), iend, ostream_iterator<int>( out, " " ) );
		out<<'\n';
	}
	return EXIT_SUCCESS;
}