Pagini recente » Cod sursa (job #1356017) | Cod sursa (job #701237) | Cod sursa (job #2937102) | Cod sursa (job #997195) | Cod sursa (job #752060)
Cod sursa(job #752060)
#include<stdio.h>
#include<vector>
#include<stack>
#define maxn 100005
#define pb push_back
using namespace std;
FILE*f=fopen("ctc.in","r");
FILE*g=fopen("ctc.out","w");
int n,m,index;
int niv[maxn],lowest[maxn],viz[maxn],inSt[maxn];
vector<int>G[maxn],v;
vector< vector<int> >C;
stack<int>St;
inline void citire () {
fscanf(f,"%d %d",&n,&m);
int x,y;
for ( int i = 1 ; i <= m ; ++i ){
fscanf(f,"%d %d",&x,&y);
G[x].pb(y);
}
}
inline void componenta ( int x ){
v.clear();
int nod;
do{
nod = St.top(); inSt[nod] = 0; St.pop();
v.pb(nod);
}while(nod != x);
C.pb(v);
}
void dfs ( int nod ){
viz[nod] = 1; St.push(nod); inSt[nod] = 1;
++index;
niv[nod] = lowest[nod] = index;
for ( vector<int>::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){
int nodvcn = (*itt);
if ( !viz[nodvcn] ){
dfs(nodvcn);
lowest[nod] = min(lowest[nod],lowest[nodvcn]);
}
else{
if ( inSt[nodvcn] ){
lowest[nod] = min(lowest[nod],lowest[nodvcn]);
}
}
}
if ( lowest[nod] == niv[nod] ){
componenta(nod);
}
}
inline void ctc () {
for ( int i = 1 ; i <= n ; ++i ){
if ( !viz[i] ){
dfs(i);
}
}
fprintf(g,"%d\n",C.size());
for ( int i = 0 ; i < C.size() ; ++i ){
for ( vector<int>::iterator itt = C[i].begin() ; itt != C[i].end() ; ++itt ){
fprintf(g,"%d ",*itt);
}
fprintf(g,"\n");
}
}
int main () {
citire();
ctc();
fclose(f);
fclose(g);
return 0;
}