Cod sursa(job #1050058)

Utilizator poparazvan1511Popa Razvan poparazvan1511 Data 8 decembrie 2013 02:30:24
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
// bfs.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<queue>
#include<vector>

using namespace std;

ifstream f("bfs.in");
ofstream g("bfs.out");

int  i, nod, m, n, s, x, y;
vector <int> A[100001], viz(100001, -1);


void BFS(int nod)
{
	queue <int> Q;
	Q.push(nod);
	viz[nod] = 0;
	while (Q.empty() == false)
	{
		for (int i = 0; i < A[Q.front()].size(); i++)
			if (viz[A[Q.front()][i]] == -1)
			{
				Q.push(A[Q.front()][i]);
				viz[A[Q.front()][i]] = viz[Q.front()] + 1;
			}
		Q.pop();
	}
}

int main()
{
	f >> n;
	f >> m;
	f >> s;
	for (i = 1; i <= m; i++)
	{
		f >> x >> y;
		A[x].push_back(y);
	}
	BFS(s);
	for (i = 1; i <= n; i++) g << viz[i] << " ";
	return 0;
}