Cod sursa(job #729817)

Utilizator gyeresihunorGyeresi Hunor gyeresihunor Data 30 martie 2012 11:30:36
Problema BFS - Parcurgere in latime Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

vector<long long> v[100001];
long long t[100001];
queue<long long> q;
queue<long long> w;
long long n,m,s;
long long ki;
long long d;
long long i;
long long a,b;

int main()
{
	freopen("bfs.in","r",stdin);
	freopen("bfs.out","w",stdout);
	scanf("%lld%lld%lld",&n,&m,&s);
	for(i=1;i<=m;i++)
	{
		scanf("%lld%lld",&a,&b);
		v[a].push_back(b);
	}
	q.push(s);
	w.push(0);
	while(!q.empty())
	{
		ki=q.front();
		d=w.front();
		q.pop();
		w.pop();
		t[ki]=d;
		vector<long long>::iterator it;
		for(it=v[ki].begin();it!=v[ki].end();++it)
		{
			if(t[*it]==0&&*it!=s)
			{
				q.push(*it);
				w.push(d+1);
			}
		}
	}
	for(i=1;i<=n;i++)
	{
		printf("%lld ", (t[i]==0?(i==s?0:-1):t[i]) );
	}
	return 0;
}