Cod sursa(job #2864272)

Utilizator RK13Barbu Eduard RK13 Data 7 martie 2022 18:49:18
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include<bits/stdc++.h>

using namespace std;

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

int n,m;
vector <int> a[100001];
queue <int> pq;
int niv[100001];

void bfs(int sol)
{int aux,l;
pq.push(sol);
while (!pq.empty()) {aux=pq.front();
                    pq.pop();
                    l=a[aux].size();
                    for (int i=0;i<l;i++) if (niv[a[aux][i]]==0) pq.push(a[aux][i]),niv[a[aux][i]]=niv[aux]+1;
                    }
}

int main()
{int m,x,sol,y;
f>>n>>m>>sol;
for (int i=1;i<=m;i++) f>>x>>y,a[x].push_back(y);
niv[sol]=1;
bfs(sol);
for (int i=1;i<=n;i++) g<<niv[i]-1<<' ';
}