Cod sursa(job #2407019)

Utilizator richard26Francu Richard richard26 Data 16 aprilie 2019 13:30:13
Problema BFS - Parcurgere in latime Scor 0
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");
vector <int> a[100001];
int dist[100001];
int viz[100001];
queue <int> q;
int main()
{
    int first_node, n, m;
    f>>n>>m>>first_node;

    for (size_t i = 0; i < m; i++)
    {
        int x, y;
        f>>x>>y;
        a[x].push_back(y);

    }
    q.push(first_node)
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        for (auto it : a[x])
        {
            q.push(it);
            dist[it]++;
        }
    }

    for (size_t i = 1; i <= n; i++) {
        if(dist[i] != 0 || i == first_node) g<<dist[i]<<" ";
            else g<<"-1 ";

    }

    return 0;
}