Pagini recente » Cod sursa (job #1631326) | Cod sursa (job #1026477) | Cod sursa (job #3178174) | Cod sursa (job #874808) | Cod sursa (job #3241272)
#include "bits/stdc++.h"
std :: vector < std :: set < int > > adj;
std :: vector < int > ans;
std :: bitset < 100005 > vis;
int n, m, target;
inline static void AddEdge(int x, int y){
adj[x].insert(y);
}
inline static void BFS(int target){
std :: queue < int > q;
ans[target] = 0;
q.push(target);
while(q.empty() == false){
int node = q.front();
q.pop();
for(auto i : adj[node]){
if(ans[i] == -1){
vis[i] = true;
ans[i] = ans[node] + 1;
q.push(i);
}
}
}
}
inline static void Solve(){
std :: cin >> n >> m >> target;
adj.resize(n + 1);
ans.resize(n + 1, - 1);
while(m -- ){
int x, y;
std :: cin >> x >> y;
AddEdge(x, y);
}
BFS(target);
for(int i = 1; i < ans.size(); i++){
std :: cout << ans[i] << ' ';
}
}
signed main(){
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
std :: ios_base :: sync_with_stdio(false);
std :: cin.tie(0);
std :: cout.tie(0);
Solve();
return 0;
}