Cod sursa(job #2487583)

Utilizator MithrilBratu Andrei Mithril Data 4 noiembrie 2019 22:55:25
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

#define NMAX 100001

using namespace std;

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

int dist[NMAX];
vector<int> graph[NMAX];
queue<pair<int, int>> q;

int main()
{
  int n,m,s;
  fin >> n >> m >> s;
  while(m--)
  {
    int x, y;
    fin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  for(int i = 1; i <= n; i++) dist[i] = -1;
  q.push({s, 0});
  while(!q.empty())
  {
    int node = q.front().first;
    int d = q.front().second;
    dist[node] = d;
    q.pop();
    for(auto x : node)
    {
      if(dist[node] == -1)
      {
        q.push({node, d+1});
      }
    }
  }

  for(int i = 1; i <= n; i++) fout << dist[i] << ' ';
  fin.close();
  fout.close();

  return 0;
}