Pagini recente » Cod sursa (job #1451564) | Cod sursa (job #1424829) | Rating Teodora (teodora_laura) | Cod sursa (job #2856307) | Cod sursa (job #1038527)
//VASS PETER 2013-11-21
//N M S
#include <vector>
#include <fstream>
#include <queue>
#include <map>
#include <list>
using namespace std;
int main(){
ifstream ifs("bfs.in");
ofstream ofs("bfs.out");
int N,M,S;
ifs>>N>>M>>S;S--;
vector<list<int> > g(N);
vector<int> o(N,-1);
for(int i=0;i<M;i++){
int x,y;
ifs>>x>>y;
if(x!=y) g[x-1].push_back(y-1);
}
queue<int> q;
q.push(S);
o[S]=0;
while(!q.empty()){
int f=q.front();
for(list<int>::iterator it=g[f].begin();it!=g[f].end();it++){
if(o[*it]==-1){
o[*it]=o[f]+1;
q.push(*it);
}
}
q.pop();
}
for(int i=0;i<N;i++)
ofs<<o[i]<<" ";
}