Pagini recente » Cod sursa (job #2061294) | Cod sursa (job #33877) | Cod sursa (job #1236707) | Cod sursa (job #842965) | Cod sursa (job #1747924)
#include<bits/stdc++.h>
#define in f
#define out cout
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int MAXN = 100001;
vector<int> G[MAXN];
int n, m, s;
int depth[MAXN];
void read() {
in >> n;
in >> m;
in >> s;
int x, y;
for(int i = 1; i <= m; i++) {
in >> x;
in >> y;
G[x].push_back(y);
}
}
void bfs(int node) {
queue<int> q;
q.push(node);
depth[node] = 0;
while(q.empty() == false) {
node = q.front();
q.pop();
for(auto vertex : G[node]) {
if(depth[vertex] == -1) {
q.push(vertex);
depth[vertex] = depth[node] + 1;
}
}
}
}
int main() {
read();
for(int i = 0; i <= n; i++) {
depth[i] = -1;
}
bfs(s);
for(int i = 1; i <= n; i++) {
out << depth[i] << " ";
}
return 0;
}