Pagini recente » Cod sursa (job #696473) | Cod sursa (job #2760660) | Cod sursa (job #1866404) | Cod sursa (job #1873760) | Cod sursa (job #2817441)
#include <bits/stdc++.h>
constexpr size_t MAXNODE = 100000;
std::array<std::vector<int>, MAXNODE> edges;
std::array<bool, MAXNODE> vis;
std::array<int, MAXNODE> nb_of_arcs_to;
void
bfs (int start) {
std::queue<int> q({start});
vis[start] = true;
nb_of_arcs_to[start] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto to: edges[now])
if (!vis[to]) {
nb_of_arcs_to[to] = nb_of_arcs_to[now] + 1;
q.emplace(to);
vis[to] = true;
}
}
}
int main () {
int n, m, s;
std::ifstream f("bfs.in");
std::ofstream g("bfs.out");
f >> n >> m >> s;
-- s;
for (int i = 0; i != m; ++ i) {
int from, to;
f >> from >> to;
-- from, -- to;
edges[from].emplace_back(to);
}
nb_of_arcs_to.fill(-1);
bfs(s);
for (int i = 0; i != n; ++ i)
g << nb_of_arcs_to[i] << ' ';
}