Cod sursa(job #2389556)

Utilizator andreibudoiAndrei Budoi andreibudoi Data 27 martie 2019 11:20:48
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#if 1
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
using namespace std;
int viz[100001];
vector<int>graph[100001];
int a[1000001];
queue <int> myq;

void bfs(int nod)
{
	a[nod] = 1;
	viz[nod] = 1;

	myq.push(nod);
	while (!myq.empty())
	{
		int x = myq.front();
		myq.pop();

		//cout<<x<<" ";
		int lim = graph[x].size();
		for (int i = 0; i < lim; i++)
			if (viz[graph[x][i]] == 0)
			{
				myq.push(graph[x][i]);
				viz[graph[x][i]] = 1;
				a[graph[x][i]] = a[x] + 1;
			}

	}

}

int main()
{
	int n, m, s;
	ifstream f("bfs.in");
	ofstream g("bfs.out");
	f >> n >> m >> s;
	int k = 0;
	for (int i = 1; i <= m; i++)
	{
		int a, b;
		f >> a >> b;
		graph[a].push_back(b);
		// graph[b].push_back(a);
	}
	bfs(s);
	for (int i = 1; i <= n; i++)
		g << a[i] - 1 << " ";


	return 0;
}
#endif