Pagini recente » Cod sursa (job #1076255) | Cod sursa (job #2208402) | Cod sursa (job #120878) | Cod sursa (job #1120237) | Cod sursa (job #1184975)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
vector <int> g[100005];
queue <int> q;
int viz[100005];
int n,m,s;
void citire()
{
scanf("%d %d %d",&n,&m,&s);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
if(x!=y)
g[x].push_back(y);
}
}
void parc()
{ int i;
q.push(s);
while(!q.empty())
{
i=q.front();
q.pop();
for(vector <int>::iterator it=g[i].begin();it!=g[i].end();it++)
if(viz[*it]==0)
{q.push(*it);
viz[*it]=viz[i]+1;}
}
viz[s]=0;
for(int i=1;i<=n;i++)
if(viz[i]==0)
printf("-1 ");
else printf("%d ",viz[i]);
}
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
citire();
parc();
return 0;
}