Cod sursa(job #1051376)

Utilizator mikeshadowIon Complot mikeshadow Data 9 decembrie 2013 22:58:17
Problema BFS - Parcurgere in latime Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
 
#define min(a,b) ((a<b)?a:b)
#define max(a,b) ((a<b)?b:a)
#define abs(a) ((a<0)?-a:a)
#define REP(i,a,b) \
	for (int i=a; i<=b; i++)
 
#define INF 1000001
 
using namespace std;
 

#ifndef TEST
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
#else
ifstream fin ("input.txt");
ofstream fout ("output.txt");
#endif
 
#define MAXN 100000

typedef long long ll;

int n,m,s;

vector<int> a[MAXN];

int r[MAXN];

void BFS(int x)
{
	queue<int> q;
	q.push(x);
	while (!q.empty())
	{
		int t;
		t = q.front();
		q.pop();
		REP(i, 0, a[t].size()-1)
			if (r [ a[t][i] ] ==INF)
			{
				q.push(a[t][i]);
				r[ a[t][i] ] = r[ t ] +1;
			}
	}
}

int main()
{
	fin>>n>>m>>s;

	REP(i,0,n-1)
		r[i] = INF;
	r[--s] = 0;
	REP(i,0,m-1)
	{
		int x,y;
		fin>>x>>y;
		x--;
		y--;
		a[x].push_back(y);
	}

	BFS(s);

	REP(i,0,n-1)
		fout<<((r[i]==INF)?-1:r[i])<<' ';
    return 0;
}