Pagini recente » Cod sursa (job #1344453) | Cod sursa (job #2558819) | Cod sursa (job #2149645) | Cod sursa (job #2785941) | Cod sursa (job #1167262)
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int N=100000;
queue<int>q;
vector<int>edges[N+1];
int bfsV[N+1];
bool vis[N+1];
int n,m,s;
void scan(){
int i,v1,v2;
scanf("%d%d%d",&n,&m,&s);
for(i=1;i<=m;i++){
scanf("%d%d",&v1,&v2);
edges[v1].push_back(v2);
}
}
void init(){
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
scan();
}
void bfsF(){
int i,father,son;
q.push(s);
vis[s]=true;
while(!q.empty()){
father=q.front();
for(i=0;i<edges[father].size();i++){
son=edges[father][i];
if(!vis[son]){
vis[son]=true;
bfsV[son]=bfsV[father]+1;
q.push(son);
}
}
q.pop();
}
}
void print(){
int i;
for(i=1;i<=n;i++){
if(i==s){
printf("0 ");
continue;
}
if(bfsV[i]==0){
printf("-1 ");
continue;
}
printf("%d ",bfsV[i]);
}
}
int main(){
init();
bfsF();
print();
return 0;
}