Pagini recente » Cod sursa (job #2054926) | Cod sursa (job #2437637) | Cod sursa (job #480813) | Cod sursa (job #1008961) | Cod sursa (job #2388246)
#include <bits/stdc++.h>
using namespace std;
#define cin in
#define cout out
ifstream in("bfs.in");
ofstream out("bfs.out");
vector<vector<int>> graph;
vector<int> dist;
vector<bool> vis;
void bfs(int start){
queue<pair<int,int>> q;
q.push({start, 0});
vis[start] = true;
while(!q.empty()){
pair<int,int> x = q.front();
q.pop();
int node = x.first;
int d = x.second;
dist[node] = d;
for(auto neigh : graph[node]){
if (!vis[neigh]){
vis[neigh] = true;
q.push({neigh,d + 1});
}
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, s, x, y;
cin >> n >> m >> s;
graph.resize(n + 1);
dist.resize(n + 1, -1);
vis.resize(n + 1, false);
while(m--){
cin >> x >> y;
graph[x].push_back(y);
}
bfs(s);
for(int i = 1; i <= n; ++i)
cout << dist[i] << ' ';
}