Pagini recente » Cod sursa (job #2639692) | Cod sursa (job #1756622) | Cod sursa (job #2658491) | Cod sursa (job #929473) | Cod sursa (job #998307)
Cod sursa(job #998307)
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int kinf = 100005;
int dist[100005];
vector<int> graph[100005];
void bfs(int source){
dist[source] = 0;
queue<int> q;
q.push(source);
while(!q.empty()){
int now = q.front();
q.pop();
for(int i = 0; i < graph[now].size(); ++i)
if(dist[graph[now][i]] > dist[now] + 1){
dist[graph[now][i]] = dist[now] + 1;
q.push(graph[now][i]);
}
}
}
int main(){
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
memset(dist, 1337, sizeof(dist));
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
for(int i = 1; i <= m; ++i){
int x, y;
scanf("%d%d", &x, &y);
graph[x].push_back(y);
}
bfs(s);
for(int i = 1; i <= n; ++i)
if(dist[i] < kinf)
printf("%d ", dist[i]);
else
printf("-1 ");
return 0;
}