Pagini recente » Cod sursa (job #2654308) | Cod sursa (job #3282119) | Cod sursa (job #3167287) | Cod sursa (job #1643249) | Cod sursa (job #2979516)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<vector<int>> v;
int bfs(int nod, int nd, int n) {
if(nod == nd) {
return 0;
}
vector<bool> viz;
viz.resize(n+5);
queue<pair<int,int>> q;
q.emplace(nod, 1);
while(!q.empty()) {
pair<int,int> now = q.front();
q.pop();
for(auto x : v[nod]) {
if(x == nd) {
return now.second;
}
if(!viz[x]) {
viz[x] = true;
q.emplace(x, now.second+1);
}
}
}
return -1;
}
int main() {
int n, m, s; fin >> n >> m >> s;
v.resize(n+5);
for(int i = 1; i <= m; i++) {
int x, y; fin >> x >> y;
v[x].emplace_back(y);
}
for(int i = 1; i <= n; i++) {
fout << bfs(s, i, n) << " ";
}
return 0;
}