Pagini recente » Istoria paginii utilizator/hynduf | Cod sursa (job #1462783) | Cod sursa (job #247989) | Cod sursa (job #687556) | Cod sursa (job #456469)
Cod sursa(job #456469)
/*
* 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 nrc, ii;
int idx[Nmax], lowlink[Nmax];
stack< pr > S;
bitset< Nmax > uz;
vector< int > G[Nmax], BCC[Nmax];
inline void GetBCC( pr x )
{
static pr y;
++nrc;
do
{
y=S.top(); S.pop();
if( ! uz.test(y.first) )
{
uz.set(y.first);
BCC[nrc].push_back(y.first);
}
if( !uz.test(y.second) )
{
uz.set(y.second);
BCC[nrc].push_back(y.second);
}
}while( y != x );
uz&=0;
}
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 N, 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';
if( !nrc )
return 0;
for( x=1; x <= nrc; ++x )
{
copy( BCC[x].begin(), BCC[x].end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
}
return (EXIT_SUCCESS);
}