Cod sursa(job #2666040)

Utilizator andreizZenoveiov Andrei andreiz Data 31 octombrie 2020 18:42:27
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include<bits/stdc++.h>

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

int n, m, root;
int q[100010], c[100010], sze[100010];
int qLength;
vector<int> v[100010];




void bfs(int nod)
{
    int i,j;
    memset(c, -1, sizeof(c));

    qLength = 1;
    c[nod] = 0;
    q[qLength] = nod;

    for(i = 1; i <= qLength; ++i)
        for(j = 0; j < sze[q[i]]; ++j)
            if(c[v[q[i]][j]] == -1)
            {
                q[++qLength] = v[q[i]][j];
                c[q[qLength]] = c[q[i]] + 1;
            }
}

int main()
{
int x, y, i;
f >> n >> m >> root;
for(i = 1; i <= m; ++i)
    {
    f >> x >> y;
    v[x].push_back(y);
    }
for(i = 1; i <= n; ++i)
    sze[i] = v[i].size();

bfs(root);

for(i = 1; i <= n; ++i)
    g << c[i] << ' ';
return 0;
}