Cod sursa(job #3300119)

Utilizator tedicTheodor Ciobanu tedic Data 12 iunie 2025 21:14:01
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;
int const N=100005;
int noduri, arce, dist[N], target;
vector<int>arc[N];
queue<int>q;
void bfs(int nod)
{
    dist[nod]=0;
    q.push(nod);
    while(!q.empty())
    {
        int nod_curent=q.front();
        q.pop();
        for(auto i:arc[nod_curent])
        if(dist[i]==N)
            dist[i]=dist[nod_curent]+1, q.push(i);
    }
}
int main()
{
    ifstream cin("bfs.in");
    ofstream cout("bfs.out");
    for(int i=1; i<=100000; i++)
        dist[i]=N;
    cin>>noduri>>arce>>target;
    int x, y;
    for(int i=1; i<=arce; i++)
    {
        cin>>x>>y;
        arc[x].push_back(y);
    }
    bfs(target);
    for(int i=1; i<=noduri; i++)
        if(dist[i]==N)
        dist[i]=-1;
    for(int i=1; i<=noduri; i++)
        cout<<dist[i]<<" ";
    return 0;
}