Cod sursa(job #335648)

Utilizator alex.cepoiAlexandru Cepoi alex.cepoi Data 30 iulie 2009 20:12:49
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#define nmax 100002

int N,M,S;
int sol[nmax];
int tmp[nmax];
vector <int> V[nmax];

void bf ()
{
	int i=1; sol[S]=1;
	tmp[0]=1; tmp[i]=S;

	while (tmp[i])
	{
		for (vector<int>::iterator j=V[tmp[i]].begin(); j!=V[tmp[i]].end(); ++j)
			if (!sol[*j])
			{
				tmp[++tmp[0]]=*j;
				sol[*j] = sol[tmp[i]]+1;
			}
		i++;
	}
}

int main ()
{
	ifstream in("bfs.in");
	ofstream out("bfs.out");

	in>>N>>M>>S;
	while (M--)
	{
		int a,b; in>>a>>b;
		V[a].push_back(b);
	}

	bf();
	for (int i=1; i<=N; ++i) out<<sol[i]-1<<' ';
	return 0;
}