Cod sursa(job #676064)

Utilizator stephy_yoyoIonescu Stefania stephy_yoyo Data 8 februarie 2012 17:21:15
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
# include <cstdio>
# include <vector>

using namespace std;

vector <int> muchie[100005];
int n, m, s, in, sf;
int coada[100010], fr[100010], distanta[100010];

int main ()
{
	freopen ("bfs.in","r",stdin);
	freopen ("bfs.out","w",stdout);
	
	scanf ("%d%d%d",&n,&m,&s);
	int a, b;
	for (int i = 1; i <= m; i ++)
	{
		scanf ("%d%d", &a, &b);
		muchie[a].push_back(b);
	}
	
	coada[0] = s;
	distanta[s] = 1;
	fr[s] = 1;
	int nod;
	for ( ; in <= sf ; in ++)
		for (int i=0; i < muchie[ coada[in] ].size(); i ++)
		{
			nod = muchie[ coada[in] ][i];
			if ( !fr[nod])
			{
				coada[++sf] = nod;
				fr[nod] = 1;
				distanta[nod] = distanta[ coada[in] ] + 1;
			}
		}
	
	for (int i=1; i <= n; i++)
		printf ("%d ",distanta[i] - 1);
	
	
	return 0;
}