Pagini recente » Cod sursa (job #2762069) | Cod sursa (job #2236534) | Cod sursa (job #1877315) | Cod sursa (job #2934077) | Cod sursa (job #976805)
Cod sursa(job #976805)
#include<cstdio>
#include<vector>
#include<iterator>
#define NMAX 100000+5
using namespace std;
vector<int> V[NMAX];
vector<int> Vt[NMAX];
vector<int> postordine;
vector<int> ctc[NMAX];
int N,M,a,b,c,cnt;
bool viz[NMAX];
void dfs(int x)
{
vector<int>::iterator it;
viz[x]=1;
for(it=V[x].begin(); it!=V[x].end(); it++)
if(!viz[*it]) dfs(*it);
postordine.push_back(x);
}
void dfst(int x)
{
vector<int>::iterator it;
viz[x]=0;
ctc[cnt].push_back(x);
for(it=Vt[x].begin(); it!=Vt[x].end(); it++)
if(viz[*it]) dfst(*it);
}
void afisare(int x)
{
vector<int>::iterator it;
for(it=ctc[x].begin();it!=ctc[x].end();it++)
printf("%d ",*it);
printf("\n");
}
int main()
{
int i;
freopen("ctc.in","r",stdin);
freopen("ctc.out","w",stdout);
scanf("%d%d",&N,&M);
for(; M; --M)
{
scanf("%d%d",&a,&b);
V[a].push_back(b);
Vt[b].push_back(a);
}
for(i=1; i<=N; i++)
if(!viz[i]) dfs(i);
vector<int>::reverse_iterator it;
for(it=postordine.rbegin();it!=postordine.rend();it++)
{
if(!viz[*it]) continue;
cnt++;
dfst(*it);
}
printf("%d\n",cnt);
for(i=1;i<=cnt;i++)
afisare(i);
return 0;
}