Pagini recente » Cod sursa (job #3259809) | Cod sursa (job #2152203) | Cod sursa (job #1536504) | Cod sursa (job #985821) | Cod sursa (job #2256484)
#include <bits/stdc++.h>
#define NMAX 100010
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
vector<int> G[NMAX],comp[NMAX];
int n,m;
int nrcomp,nr;
bitset <NMAX> viz;
int ord[NMAX], low[NMAX],pas=0;
stack <int> stk;
void Tarjan(int x){
ord[x] = low [x] = ++pas;
viz[x]=true;
stk.push(x);
for(auto it: G[x]){
if(ord[it])
if(viz[it])
low[x]=min(low[x],low[it]);
else;
else{
Tarjan(it);
low[x]=min(low[x],low[it]);
}
}
if(low[x]==ord[x]){
nrcomp++;
int stktop;
do{
stktop=stk.top();
stk.pop();
viz[stktop]=false;
comp[nrcomp].push_back(stktop);
}while(stktop != x);
}
}
void citire(){
f>>n>>m;
for(int i=1,x,y;i<=m;i++){
f>>x>>y;
G[x].push_back(y);
}
}
void afisare(){
g<<nrcomp<<"\n";
for(int i=1;i<=nrcomp;i++){
for(int j=0;j<comp[i].size();j++){
g<<comp[i][j]<<" ";
}
g<<"\n";
}
}
int main()
{
citire();
for(int i=1;i<=n;i++)
if(!ord[i])Tarjan(i);
afisare();
return 0;
}