Cod sursa(job #2259587)

Utilizator PaulRPFRebenciuc Paul-Florin PaulRPF Data 13 octombrie 2018 15:20:58
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
#define Nmax 100002
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int N,M,X,x,y,k,v,node;
vector <int>Neighbour[Nmax];
queue <int>c;
vector <int>used(Nmax,-1);

void bfs()
{
    c.push(X);used[X]=0;
    while(!c.empty())
    {
        v=c.front();c.pop();
        for(k=0;k<Neighbour[v].size();k++)
        {
            node=Neighbour[v][k];
            if(used[node]==-1)
                used[node]=used[v]+1,c.push(node);
        }
    }
}
int main()
{
    f>>N>>M>>X;
    while(f>>x>>y)Neighbour[x].push_back(y);

    bfs();
    for(k=1;k<=N;k++)g<<used[k]<<" ";
    return 0;
}