Cod sursa(job #627520)

Utilizator the_snyper06FMI - ALexandru Mihai the_snyper06 Data 30 octombrie 2011 03:35:46
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include<cstdio>
#include<vector>

using namespace std;

int n, m, s;
vector <int>L[100001], st;
bool viz[100001];
int nivel[100001];

void BFS(int s)
{
	int pr, ul;
	unsigned int i;
	
	pr = ul = 0;
	st.push_back(s);
	nivel[s] = 0;
	viz[s] = true;
	
	while(pr <= ul)
	{
		s = st[pr++];
		for(i = 0; i < L[s].size(); i++)
		{
			int x = L[s][i];
			if(!viz[x])
			{
				++ul;
				st.push_back(x);
				viz[x] = true;
				nivel[x] = nivel[s] + 1;
			}
		}
	}
}

int main()
{
	int i, x, y;
	
	freopen("bfs.in", "r", stdin);
	freopen("bfs.out", "w", stdout);
	
	scanf("%d %d %d", &n, &m, &s);
	for(i = 1; i <= m; i++)
	{
		scanf("%d %d", &x, &y);
		L[x].push_back(y);
	}
	
	BFS(s);
	
	for(i = 1; i <= n; i++)
	{
		if(nivel[i] == 0 && i != s) nivel[i] = -1;
		printf("%d ", nivel[i]);
	}
	
	return 0;
}