Cod sursa(job #550424)

Utilizator BitOneSAlexandru BitOne Data 9 martie 2011 15:28:19
Problema Componente biconexe Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <stack>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define N_MAX 100011
#define pr pair< int, int >
#define mkpr make_pair< int, int >

using namespace std;
int N, _indexDFS, cbLength;
int indexDFS[N_MAX], lowLink[N_MAX];
stack< pr > S;
vector< int > G[N_MAX], CB[N_MAX];
inline int _min( int x, int y ) { return ( x <= y ? x : y ); }
inline void DFS( int x )
{
	indexDFS[x]=lowLink[x]=++_indexDFS;
	vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
	for( ; it < iend; ++it )
		if( 0 == indexDFS[*it] )
		{
			S.push( mkpr( x, *it ) );
			DFS( *it );
			lowLink[x]=_min( lowLink[x], lowLink[*it] );
			if( lowLink[*it] >= indexDFS[x] )
			{
				vector< int > was( N+1, 0 );
				pr y, p0=mkpr( x, *it );
				do
				{
					y=S.top(); S.pop();
					if( false == was[y.first] )
					{
						CB[cbLength].push_back(y.first);
						was[y.first]=true;
					}
					if( false == was[y.second] )
					{
						CB[cbLength].push_back(y.second);
						was[y.second]=true;
					}
				}while( y != p0 );
				++cbLength;
			}
		}
		else lowLink[x]=_min( lowLink[x], indexDFS[*it] );
}
int main( void )
{
	int M, x, y;
	ifstream in( "biconex.in" );
	for( in>>N>>M; M; --M )
	{
		in>>x>>y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	for( x=1; x <= N; ++x )
		if( 0 == indexDFS[x] )
			DFS(x);
	ofstream out( "biconex.out" );
	out<<cbLength<<'\n';
	for( x=0; x < cbLength; ++x )
	{
		copy( CB[x].begin(), CB[x].end(), ostream_iterator<int>( out, " " ) );
		out<<'\n';
	}
	return EXIT_SUCCESS;
}