Cod sursa(job #292415)

Utilizator spidyvenomMarius Toma spidyvenom Data 31 martie 2009 09:38:47
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<fstream.h>
#define nmax 100001
ifstream f("ctc.in");
ofstream g("ctc.out");
long n,m,k,nr,t[nmax],v[nmax];
struct nod
{
long z;
nod *adr;
};
nod *l[nmax],*at[nmax],*sol[nmax];

void citire()
{
long x,y;
f>>n>>m;
for(long i=0;i<m;i++)
	{
	f>>x>>y;
	nod *p=new nod;
	p->z=y;
	p->adr=l[x];
	l[x]=p;
	p=new nod;
	p->z=x;
	p->adr=at[y];
	at[y]=p;
	}
}

void df1(long x)
{
for(nod *p=l[x];p;p=p->adr)
	if(v[p->z]==0)
	{
	v[p->z]=1;
	df1(p->z);
	}
k++;
t[k]=x;
}

void df2(long x,long nr)
{
nod *q=new nod;
q->z=x;
q->adr=sol[nr];
sol[nr]=q;
v[x]=-1;
for(nod *p=at[x];p;p=p->adr)
	if(v[p->z]==1) df2(p->z,nr);
}

void afisare()
{
g<<nr<<'\n';
for(long i=1;i<=nr;i++)
	{
	while(sol[i])
		{
		g<<sol[i]->z<<" ";
		sol[i]=sol[i]->adr;
		}
	g<<'\n';
	}
}

int main()
{
citire();
long i;
for(i=1;i<=n;i++)
	if(!v[i])
	{
	v[i]=1;
	df1(i);
	}
for(i=n;i>=1;i--)
	if(v[t[i]]==1)
	{
	nr++;
	df2(t[i],nr);
	}
afisare();
g.close();
return 0;
}