Pagini recente » Cod sursa (job #377742) | Cod sursa (job #2216969) | Cod sursa (job #3343852) | Cod sursa (job #941840) | Cod sursa (job #3331968)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
#define pb push_back
vector<int> v[100001],ct[100001];
int onst[100001],id[100001],low[100001],idcurent,cate;
stack<int> s;
void tarjan(int n){
onst[n]=1;
id[n]=low[n]=++idcurent;
s.push(n);
for(int i:v[n]){
if(!id[i])
tarjan(i);
if(onst[i])
low[n]=min(low[n],low[i]);
}
if(low[n]==id[n]){
int top;
cate++;
do{
top=s.top();
ct[cate].pb(top);
low[top]=id[n];
onst[top]=0;
s.pop();
}while(top!=n);
}
}
int main(){
int n,m;
fin>>n>>m;
for(int i=1;i<=m;++i){
int x,y;
fin>>x>>y;
v[x].pb(y);
}
for(int i=1;i<=n;++i){
if(!id[i])
tarjan(i);
}
fout<<cate<<'\n';
for(int i=1;i<=cate;++i){
for(int j:ct[i])
fout<<j<<" ";
fout<<'\n';
}
}