Pagini recente » Cod sursa (job #2704358) | Cod sursa (job #2814703) | Cod sursa (job #2706115) | Cod sursa (job #3165921) | Cod sursa (job #1184581)
#include<fstream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
queue<pair<int,int> > q;
int i,j,k,n,m,a,b,nr,s,infi=1000000;
vector<int> v[100001];
int val[100001];
pair<int,int> x;
int main(){
cin>>n>>m>>s;
for(i=1;i<=m;i++){
cin>>a>>b;
v[a].push_back(b);
}
bool bl=0;
for(j=1;j<=n;j++){
val[j]=-1;
}
q.push(make_pair(s,0));
while(!q.empty()){
x=q.front();
q.pop();
if(val[x.first]==-1){
val[x.first]=x.second;
}
for(k=0;k<v[x.first].size();k++){
if(val[v[x.first][k]]==-1){
q.push(make_pair(v[x.first][k],x.second+1));
}
}
}
for(i=1;i<=n;i++){
cout<<val[i]<<' ';
}
return 0;
}