Pagini recente » Cod sursa (job #415331) | Cod sursa (job #1254740) | Cod sursa (job #584543) | amcbn | Cod sursa (job #3215940)
#include <bits/stdc++.h>
using namespace std;
int n,m,x;
vector<int>G[100001];
int d[100001];
ifstream fin("bfs.in");
ofstream fout("bfs.out");
queue<int>Q;
int main(){
fin>>n>>m>>x;
while(m--){
int i,j; fin>>i>>j;
G[i].push_back(j);
}
for(int i=1;i<=n;i++)
d[i]=1000000000;
d[x]=0;
Q.push(x);
while(!Q.empty()){
int nod = Q.front();
Q.pop();
for(auto v : G[nod])
if(d[v]>d[nod]+1){
d[v]=d[nod]+1;
Q.push(v);
}
}
for(int i=1;i<=n;i++){
if(d[i]==1000000000)
fout<<-1<<" ";
else
fout<<d[i]<<" ";
}
return 0;
}