Pagini recente » Cod sursa (job #302904) | Cod sursa (job #1665575) | Cod sursa (job #2489356) | Cod sursa (job #1692798) | Cod sursa (job #1035661)
/*
~Keep It Simple!~
*/
#include <stdio.h>
#include <list>
#include <iostream>
using namespace std;
list<int> graph[100001];
list<int> bf;
int n,m,s,cost[100001];
void bfs()
{
while(!bf.empty())
{
int s = *bf.begin();
bf.pop_front();
for( list<int>::iterator it = graph[s].begin(); it!=graph[s].end();it++)
{
if(cost[*it]==0)
{
cost[*it] = cost[s]+1;
bf.push_back(*it);
}
}
}
}
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
scanf("%d%d%d",&n,&m,&s);
bf.push_back(s);
cost[s] = 1;
int x,y;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
graph[x].push_back(y);
}
bfs();
for(int i=1;i<=n;i++)
printf("%d ",cost[i]-1);
}