Pagini recente » Monitorul de evaluare | Profil M@2Te4i | Istoria paginii utilizator/laura09 | Istoria paginii utilizator/gianniy | Cod sursa (job #460428)
Cod sursa(job #460428)
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 100111
/*
*
*/
using namespace std;
int indx, nrc;
bool isS[Nmax];
int idx[Nmax], lowlink[Nmax];
stack< int > S;
vector< int > G[Nmax], SCC[Nmax];
inline void DFS( int x )
{
S.push(x);
isS[x]=true;
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 if( isS[*it] )
lowlink[x]=min( lowlink[x], idx[*it] );
if( idx[x] == lowlink[x] )
{
int y;
++nrc;
do
{
y=S.top(); S.pop();
isS[y]=false;
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;
}