Cod sursa(job #2701852)

Utilizator beingsebiPopa Sebastian beingsebi Data 1 februarie 2021 23:10:22
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
// #define f cin
// #define g cout
vector<int> v[100009];
int n, m, s, rez[100009];
queue<int> q;
int32_t main()
{
    ios_base::sync_with_stdio(false);
    f.tie(nullptr);
    g.tie(nullptr);
    f >> n >> m >> s;
    for (int x, y; m; m--)
        f >> x >> y, v[x].emplace_back(y);
    rez[s] = 1;
    q.push(s);
    while (!q.empty())
    {
        int ac = q.front();
        q.pop();
        for (const auto &i : v[ac])
            if (!rez[i])
                rez[i] = rez[ac] + 1, q.push(i);
    }
    for (int i = 1; i <= n; i++)
        g << (!rez[i] ? -1 : rez[i] - 1) << ' ';
    return 0;
}