Pagini recente » Cod sursa (job #1785131) | Cod sursa (job #751167) | Cod sursa (job #1102534) | Cod sursa (job #297728) | Cod sursa (job #2375693)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("bfs.in");
ofstream g ("bfs.out");
int n,m,s,x,y;
int root[100005];
vector<int> v[100005];
queue <int> q;
void BFS(int nod)
{
q.push(nod);
int p=1,u=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(auto t:v[x])
if(!root[t] && t!=nod){
root[t]=root[x]+1;
q.push(t);
}
}
}
int main()
{
f>>n>>m>>s;
for(int i=1;i<=m;++i){
f>>x>>y;
v[x].push_back(y);
}
BFS(s);
for(int i=1;i<=n;++i)
if(!root[i] && i!=s)g<<-1<<' ';
else g<<root[i]<<' ';
f.close(); g.close();
return 0;
}