Cod sursa(job #2638277)

Utilizator testraxman01Sicoe Raul testraxman01 Data 27 iulie 2020 17:08:28
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <vector>
#include <queue>
#define MAXN 100005

using namespace std;

ifstream f("bfs.in");
ofstream g("bfs.out");
queue <int> Q;
vector <int> graph[MAXN];
int viz[MAXN];
int N, M, start, x, y;

void BFS(int start)
{

    int z;
    Q.push(start);
    viz[start] = 1;
	    while (!Q.empty())
        {

            z = Q.front();

            for (auto x:graph[z])
            {

            if (!viz[x])
            {

                viz[x] = viz[z] + 1;

                Q.push(x);

            }

            }
            Q.pop();
        }
}

 void Read()
{

    f >> N >> M >> start;

    for (int i = 1; i <= M; i++)
    {

        f >> x >> y;
        graph[x].push_back(y);
    }
    BFS(start);

    for (int i = 1; i <= N; i++)
    {
        g << viz[i] - 1 << " ";
    }
}
int main ()
{

    Read();
    f.close();
    g.close(); return 0;
}