Pagini recente » Cod sursa (job #916569) | Cod sursa (job #151956) | Cod sursa (job #2247103) | Cod sursa (job #177639) | Cod sursa (job #455340)
Cod sursa(job #455340)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 13, 2010, 3:40 PM
*/
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100011
/*
*
*/
using namespace std;
int nrct, indx;
bool InS[Nmax];
int idx[Nmax], lowlink[Nmax];
stack< int > S;
vector< int > G[Nmax], ctc[Nmax];
inline void GetTc( int x )
{
int v;
++nrct;
do
{
v=S.top(); S.pop();
InS[v]=false;
ctc[nrct].push_back(v);
}while( v != x );
}
inline void DFS( int x )
{
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
S.push(x);
InS[x]=true;
idx[x]=lowlink[x]=++indx;
for( ; it < iend; ++it )
if( !idx[*it] )
{
DFS(*it);
lowlink[x]=min( lowlink[x], lowlink[*it] );
}
else if( InS[*it] )
lowlink[x]=min( lowlink[x], lowlink[*it] );
if( idx[x] == lowlink[x] )
GetTc(x);
}
int main( void )
{
int N, M, x, y;
ifstream in( "ctc.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
}
for( x=1; x <= N; ++x )
if( !idx[x] )
DFS(x);
ofstream out( "ctc.out" );
out<<nrct<<'\n';
for( x=1; x <= nrct; ++x )
{
copy( ctc[x].begin(), ctc[x].end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
}
return EXIT_SUCCESS;
}