Pagini recente » Cod sursa (job #2811992) | Cod sursa (job #744752) | Cod sursa (job #2648720) | Cod sursa (job #2259572) | Cod sursa (job #456486)
Cod sursa(job #456486)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 15, 2010, 5:13 PM
*/
#include <stack>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100011
/*
*
*/
using namespace std;
typedef pair< int, int > pr;
int N ,nrc, ii;
int idx[Nmax], lowlink[Nmax];
stack< pr > S;
vector< int > G[Nmax], BCC[Nmax];
inline void GetBCC( pr x )
{
pr y;
vector< bool > uz( N );
++nrc;
do
{
y=S.top(); S.pop();
if( ! uz[y.first] )
{
uz[y.first]=true;
BCC[nrc].push_back(y.first);
}
if( !uz[y.second] )
{
uz[y.second]=true;
BCC[nrc].push_back(y.second);
}
}while( y != x );
if( !uz[x.first] )
BCC[nrc].push_back(x.first);
if( !uz[x.second] )
BCC[nrc].push_back(x.second);
}
inline void DFS( int x )
{
vector< int >::iterator it=G[x].begin(), iend=G[x].end();
idx[x]=lowlink[x]=++ii;
for( ; it < iend; ++it )
{
if( 0 == idx[*it] )
{
S.push( pr( x, *it ) );
DFS(*it);
lowlink[x]=min( lowlink[*it], lowlink[x] );
if( lowlink[*it] >= idx[x] )
GetBCC( pr( x, *it ) );
}
else lowlink[x]=min( lowlink[x], idx[*it] );
}
}
int main(int argc, char** argv)
{
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 == idx[x] )
DFS(x);
ofstream out( "biconex.out" );
out<<nrc<<'\n';
for( x=1; x <= nrc; ++x )
{
copy( BCC[x].begin(), BCC[x].end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
}
return (EXIT_SUCCESS);
}