Pagini recente » Cod sursa (job #2150841) | Cod sursa (job #1156533) | Cod sursa (job #31558) | Cod sursa (job #2907834) | Cod sursa (job #2677495)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
queue <int> q;
const int nmax=100000;
vector <int> g[nmax+1];
int d[nmax+1];
int main(){
int n,m,s;
fin>>n>>m>>s;
for(int i=1;i<=m;i++){
int x,y;
fin>>x>>y;
g[x].push_back(y);
}
q.push(s);
d[s]=1;
while(q.empty()==0){
int x=q.front();
q.pop();
for(int i=0;i<int(g[x].size());i++){
int xn=g[x][i];
if(d[xn]==0){
q.push(xn);
d[xn]=d[x]+1;
}
}
}
for(int i=1;i<=n;i++){
fout<<d[i]-1<<" ";
}
fout<<"\n";
return 0;
}