Cod sursa(job #320424)

Utilizator ProcopliucProcopliuc Adrian Procopliuc Data 4 iunie 2009 18:23:45
Problema Componente tare conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
# include <stdio.h>
int s[100001],i,j,n,m,k,x,y,x1,x2,q;
struct nod
{
int info;
nod *urm;
}*a[100001],*b[100001],*p,*v,*c[100001];


void df1 (int x)
{
nod *p;
p=a[x];
s[x]=1;
while (p)
{
if (s[p->info]==0)
df1 (p->info);
p=p->urm;
}
}




void df (int x)
{
nod *p;
s[x]=2;
p=b[x];
b[x]=0;
a[x]=0;
v=new nod;
v->info=x;
v->urm=c[k];
c[k]=v;

while (p)
{
if (s[p->info]==1)
df (p->info);


p=p->urm;
}


}

int main ()
{
freopen ("ctc.in","r",stdin);
freopen ("ctc.out","w",stdout);
scanf ("%i%i",&n,&m);
for (i=1;i<=m;i++)
{
scanf ("%i%i",&x,&y);

p=new nod;
p->info=y;
p->urm=a[x];
a[x]=p;

p=new nod;
p->info=x;
p->urm=b[y];
b[y]=p;
}
for (i=1;i<=n;i++)
{
if (s[i]==0)
df1 (i);

for (j=1;j<=n;j++)
if (s[j]==1)
{
k++;
df (j);
break;
}
for (q=j;q<=n;q++)
if (s[q]==1)
s[q]=0;

}



printf ("%i\n",k);

for (i=1;i<=k;i++)
{
while (c[i])
{
printf ("%i ",c[i]->info);
c[i]=c[i]->urm;
}
printf ("\n");
}
return 0;

}