Cod sursa(job #319943)

Utilizator ProcopliucProcopliuc Adrian Procopliuc Data 2 iunie 2009 21:41:16
Problema Componente tare conexe Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
# include <stdio.h>
int s1[100001],s2[100001],i,j,n,m,k,x,y,x1,x2;
struct nod
{
int info;
nod *urm;
}*a[100001],*b[100001],*p,*v,*c[100001];
void df1 (int x)
{
nod *p;
p=a[x];
s1[x]=i;
while (p)
{
if (s1[p->info]==0)
df1 (p->info);
p=p->urm;
}
}

void df2 (int x)
{
nod *p;
p=b[x];
s2[x]=i;
while (p)
{
if (s2[p->info]==0)
df2 (p->info);
p=p->urm;
}
}

void df (int x)
{
nod *p;
s1[x]=0;
s2[x]=0;
p=a[x];

v=new nod;;
v->info=x;
v->urm=c[k];
c[k]=v;

while (p)
{
if (s2[p->info]==x2 && s1[p->info]==x1 && s1[p->info]!=0)
{

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 (s1[i]==0)
df1 (i);
for (i=1;i<=n;i++)
if (s2[i]==0)
df2(i);
for (i=1;i<=n;i++)
if (s1[i]!=0)
{
x1=s1[i];
x2=s2[i];


k++;
df (i);


}

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;

}