Cod sursa(job #410263)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 4 martie 2010 11:07:37
Problema Componente tare conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include<stdio.h>
#define nmax 100010
int vza[nmax], vzt[nmax], tp[nmax], comp[nmax], n, m, t, k, i, nr;
struct elem
{	int vf;
	elem *nxt;
}	*ga[nmax], *gt[nmax], *sol[nmax], *p, *q;

void read()
{	int x, y, i;
	scanf("%d%d", &n, &m);
	for(i=1; i<=m; i++)
	{	scanf("%d%d", &x, &y);
		q=new elem;
		q->vf=y; q->nxt=ga[x]; ga[x]=q;
		q=new elem;
		q->vf=x; q->nxt=gt[y]; gt[y]=q;
	}
}

void dfs_a(int x, int k)
{	elem *q;

	vza[x]=1;
	q=ga[x];
	comp[x]=k;
	
	while(q)
	{	if(!vza[q->vf])
			dfs_a(q->vf, k);		
		q=q->nxt;
	}
	tp[t]=x; t++;
}

void dfs_t(int x, int k)
{	elem *q;
	
	vzt[x]=1;
	q=gt[x];

	while(q)
	{	if(!vzt[q->vf])
			dfs_t(q->vf, k);
		q=q->nxt;
	}

	if(comp[x]==k)
	{	p=new elem;
		p->vf=x;
		p->nxt=sol[nr];
		sol[nr]=p;
	}
}

int main()
{
	freopen("ctc.in", "r", stdin);
	freopen("ctc.out", "w", stdout);
	
	read();
	t=1; k++;
	for(i=1; i<=n; i++)
		if(!vza[i])
		{	dfs_a(i, k); k++;
		}
	for(t--; t>0; t--)
	{	if(!vzt[tp[t]])
		{	nr++;
			dfs_t(tp[t], comp[tp[t]]);
		}
	}
	
	printf("%d\n", k);
	for(i=1; i<=n; i++)
	{	q=sol[i];
		while(q)
		{	printf("%d ", q->vf);
			q=q->nxt;
		}
		printf("\n");
	}
	
	
	return 0;
	
}