Pagini recente » Cod sursa (job #2383235) | Cod sursa (job #1889301) | Cod sursa (job #588861) | Cod sursa (job #1148583) | Cod sursa (job #2303947)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector<int> A[100005] ,B[100005];
int n,m;
int viz1[100005];
int viz2[100005];
stack<int> S;
int ctc;
void Citire()
{
int x,y,i,j;
fin>>n>>m;
for(i=1;i<=m; ++i)
{
fin>>x>>y;
A[x].push_back(y);
B[y].push_back(x);
}
}
void DFS(int x)
{
viz1[x]=1;
for(int i=0; i< A[x].size(); ++i)
if(viz1[ A[x][i] ] != 1)
DFS( A[x][i] );
S.push(x);
}
void DFS2(int x)
{
viz2[x]=ctc;
for(int i=0; i< B[x].size(); ++i)
if(viz2[B[x][i]]==0)
DFS2(B[x][i]);
}
void Do()
{
int i,j,x;
for(i=1; i<=n; ++i)
if(viz1[i]!=1)
DFS(i);
while(!S.empty())
{
x=S.top();
if(viz2[x]==0)
{
ctc++;
DFS2(x);
}
S.pop();
}
fout<<ctc<<"\n";
for(i=1; i<=ctc; ++i)
{
for(j=1;j<=n;++j)
if(viz2[j]==i)
fout<<j<<" ";
fout<<"\n";
}
}
int main()
{
Citire();
Do();
return 0;
}