Cod sursa(job #1329346)

Utilizator MarianMMorosac George Marian MarianM Data 29 ianuarie 2015 13:57:41
Problema BFS - Parcurgere in latime Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define DMAXN 100001
#define DMAXM 1000001

int N, M, S;
queue<int> Q;

struct Nod{
	int dist = -1, parinte, desc;
	vector<int> Adj;
} G[DMAXN];

int main(){
	int i, j, x, y;

	freopen("bfs.in", "r", stdin);
	freopen("bfs.out", "w", stdout);

	cin >> N >> M >> S;

	for (i = 0; i < M; i++){
		cin >> x >> y;
		G[x].Adj.push_back(y);
	}

	G[S].dist = 0;
	Q.push(S);

	while (Q.size()){
		x = Q.front(); 
		G[x].desc = 1;
		Q.pop();
		for (i = 0; i < G[x].Adj.size(); i++){
			y = G[x].Adj[i];
			if (!G[y].desc){
				G[y].dist = G[x].dist + 1;
				G[y].parinte = x;
				Q.push(y);
			}
		}
	}

	for (i = 1; i <= N; i++)
		cout << G[i].dist << ' ';

	return 0;
}