Cod sursa(job #1705825)

Utilizator felician.todeadarius felician.todea Data 20 mai 2016 23:54:58
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <vector>
#include <fstream>
#include <queue>
using namespace std;

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

vector <int> graph[100005];
vector <bool> viz(100005, false);
vector <int> dist(100005, -1);
queue <int> q;

int main(){
	int n, m, s;
	f >> n >> m >> s;

	dist[s] = 0;
	q.push(s);
	viz[s] = true;

	int x, y;
	for(int i = 1; i <= m; i++){
		f >> x >> y;
		graph[x].push_back(y);
	}

	int node, son;
	while(!q.empty()){
		node = q.front();
		q.pop();

		for(int i = 0; i < graph[node].size(); i++){
			son = graph[node][i];
			if(!viz[son]){
				dist[son] = dist[node] + 1;
				viz[son] = true;
				q.push(son);
			}
		}
	}

	for(int i = 1; i <= n; i++)
		g << dist[i] << " ";

}