Cod sursa(job #2974293)

Utilizator IanisBelu Ianis Ianis Data 3 februarie 2023 20:06:39
Problema BFS - Parcurgere in latime Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

#ifdef LOCAL
ifstream fin("input.txt");
#define fout cout
#else
#include <bits/stdc++.h>
ifstream fin("bfs.in");
ofstream fout("bfs.out");
#endif

const int NMAX = 1e5+5;

int n, m, s;
int ans[NMAX];
bool viz[NMAX];
vector<int> g[NMAX];

void read() {
    fin >> n >> m >> s;
    for (int i = 1; i <= m; i++) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
    }
}

void solve(int x, int s) {
    viz[x] = true;
    ans[x] = s;
    for (auto &it : g[x]) {
        if (!viz[it]) {
            solve(it, s + 1);
        }
    }
}

int main() {
    read();
    solve(s, 1);
    for (int i = 1; i <= n; i++)
        fout << ans[i] - 1 << ' ';
    return 0;
}