Cod sursa(job #333349)

Utilizator crisojogcristian ojog crisojog Data 22 iulie 2009 12:58:07
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include<stdio.h>
long n,m,s;
struct nod
 {int nd;
  nod *next;};
nod *L[100010];
long q[100010];
long viz[100010];
long st, dr,i;

void add(int nr_nod) 
{
	nod *p=L[nr_nod];
	nod *c=p;
	while(c)
    {
		if(viz[c->nd]==0 && c->nd!=s)
		{
			q[++dr]=c->nd;
			viz[q[dr]]=viz[nr_nod]+1;
		}
		c=c->next;
	}
}

void lee()
{

	st=dr=1;
	q[1]=s;
	while(st<=dr)
	{
		add(q[st]);
		st++;
	}
}
void print()
{
	for(i=1;i<=n;++i)
		if(viz[i]==0 && i!=s) printf("-1 ");
		else
			printf("%ld ",viz[i]);
	printf("\n");
}
int main()
{
	long x,y;
	nod  *p;
	freopen("bfs.in","r",stdin);
	freopen("bfs.out","w",stdout);
	scanf("%ld%ld%ld",&n,&m,&s);
	for(i=1;i<=m;++i)
	{
		scanf("%ld%ld",&x,&y);
		if(x!=y)
		{
			p=new nod;  
			p->nd=y;
			p->next=L[x];
			L[x]=p;
		}
	}
	lee();
	print();
	return 0;
}