Pagini recente » Cod sursa (job #1744196) | Cod sursa (job #1684368) | Cod sursa (job #2253630) | Cod sursa (job #1893436) | Cod sursa (job #557599)
Cod sursa(job #557599)
#include<cstdio>
#include<algorithm>
#define Nmx 1000001
using namespace std;
int viz[Nmx],n,m,pre[Nmx],nr,nrs;
struct nod
{
int inf;
nod *urm;
};
nod *G[Nmx],*GT[Nmx];
struct ds
{
int p,ind;
};
ds sol[Nmx];
struct cmp
{
bool operator()(const ds &a,const ds &b)
{
return a.p<b.p;
}
};
void add(int x,int y)
{
nod *aux=new nod;
aux->inf = y;
aux->urm = G[x];
G[x] = aux;
}
void addt(int x,int y)
{
nod *aux=new nod;
aux->inf = y;
aux->urm = GT[x];
GT[x] = aux;
}
void citire()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
add(x,y);
addt(y,x);
}
}
void dfs(int x)
{
viz[x]=1;
for(nod *p=G[x];p;p=p->urm)
if(!viz[p->inf])
dfs(p->inf);
pre[++nr]=x;
}
void dfst(int x)
{
viz[x]=0;
sol[x].p=nrs;
for(nod *p=GT[x];p;p=p->urm)
if(viz[p->inf])
dfst(p->inf);
}
int main()
{
freopen("ctc.in","r",stdin);
freopen("ctc.out","w",stdout);
citire();
for(int i=1;i<=n;++i)
{
if(!viz[i])
dfs(i);
sol[i].ind=i;
}
for(int i=n;i>=1;--i)
if(viz[pre[i]])
{
++nrs;
dfst(pre[i]);
}
sort(sol+1,sol+n+1,cmp());
printf("%d\n",nrs);
int j=1;
for(int i=1;i<=nrs;++i)
{
printf("%d",sol[j].ind);
int ji=j;j++;
while(j<=n&&sol[j].p==sol[ji].p)
{
printf(" %d",sol[j].ind);
++j;
}
printf("\n");
}
return 0;
}