Pagini recente » Cod sursa (job #1476929) | Cod sursa (job #3206588) | Cod sursa (job #2619770) | Cod sursa (job #2212564) | Cod sursa (job #2168157)
#include<bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,s,x,y,viz[100001],d[100001],i;
vector <int> G[1000001];
queue <int> q;
void bfs(int nod)
{
q.push(nod);
while(!q.empty())
{
x=q.front();
viz[x]=1;
for(int i=0;i<G[x].size();i++)
{
if(!viz[G[x][i]])
{
d[G[x][i]]=d[x]+1;
q.push(G[x][i]);
}
}
q.pop();
}
}
int main()
{
f>>n>>m>>s;
for(i=1;i<=m;i++)
{
f>>x>>y;
G[x].push_back(y);
}
bfs(s);
for(i=1;i<=n;i++)
if(d[i]==0&&i!=s)
g<<-1<<" ";
else
g<<d[i]<<" ";
return 0;
}