Cod sursa(job #2922479)

Utilizator raresgherasaRares Gherasa raresgherasa Data 8 septembrie 2022 17:10:59
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("bfs.in");
ofstream fout ("bfs.out");

int main(){
  int n, m, s; fin >> n >> m >> s;
  vector<int>g[n + 1], d(n + 1, 0);
  for (int i = 0; i < m; i++){
    int x, y; fin >> x >> y;
    g[x].push_back(y);
  }
  d[s] = 1;
  queue<int>q;
  q.push(s);
  while (q.size()){
    int k = q.front();
    q.pop();
    for (int u : g[k]){
      if (d[u] == 0){
        d[u] = d[k] + 1;
        q.push(u);
      }
    }
  }
  for (int i = 1; i <= n; i++){
    fout << d[i] - 1 << ' ';
  }
}