Pagini recente » Cod sursa (job #2970331) | Cod sursa (job #1312876) | Cod sursa (job #2052218) | Cod sursa (job #1240316) | Cod sursa (job #460426)
Cod sursa(job #460426)
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100111
/*
*
*/
using namespace std;
int indx, nrc;
int idx[Nmax], lowlink[Nmax];
stack< int > S;
vector< int > G[Nmax], SCC[Nmax];
inline void DFS( int x )
{
S.push(x);
idx[x]=lowlink[x]=++indx;
vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( 0 == idx[*it] )
{
DFS( *it );
lowlink[x]=min( lowlink[x], lowlink[*it] );
}
else lowlink[x]=min( lowlink[x], idx[*it] );
if( idx[x] == lowlink[x] )
{
int y;
++nrc;
do
{
y=S.top(); S.pop();
SCC[nrc].push_back(y);
}while( y != 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( 0 == idx[x] )
DFS(x);
ofstream out( "ctc.out" );
out<<nrc<<'\n';
for( x=1; x <= nrc; ++x )
{
copy( SCC[x].begin(), SCC[x].end(), ostream_iterator< int >( out, " " ) );
out<<'\n';
}
return EXIT_SUCCESS;
}