Cod sursa(job #2288911)

Utilizator victorv88Veltan Victor victorv88 Data 24 noiembrie 2018 09:27:48
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;


queue<int> q;
vector<int>graph[100005];

ifstream f("bfs.in");
ofstream g("bfs.out");

int n, m, s,from, to,viz[100005],el;

void bfs()
{
    viz[s]=1;
    q.push(s);
    while (!q.empty())
    {
        el=q.front();
        q.pop();
        for (auto it:graph[el])
        {
            if (viz[it]==0)
            {
                viz[it]=viz[el]+1;
                q.push(it);
            }
        }
    }
}

int main() {
    f >> n >> m >> s;
    for (int i=1; i<=m; i++)
    {
        f >> from >> to;
        graph[from].push_back(to);
    }
    bfs();
    for (int i=1; i<=n; i++)
    {
        g << viz[i]-1 <<' ';
    }
    return 0;
}