Cod sursa(job #1695812)

Utilizator oanaroscaOana Rosca oanarosca Data 27 aprilie 2016 20:33:14
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
#include <queue>

using namespace std;

struct arc {
  queue<int>v;
}a[1000001];

int n, m, s, i, sol[100001], x, y;
queue <int> q;

int main () {
  ifstream fi("bfs.in");
  ofstream fo("bfs.out");
  fi >> n >> m >> s;
  for (i = 1; i <= m; i++)
    fi >> x >> y, a[x].v.push(y);
  q.push(s);
  while (!q.empty()) {
    x = q.front(); q.pop();
    while (!a[x].v.empty()) {
      y = a[x].v.front(); a[x].v.pop();
      if (not sol[y] and y != s)
        sol[y] = sol[x]+1, q.push(y);
    }
  }
  for (i = 1; i <= n; i++)
    sol[i] == 0 and i != s ? fo << "-1 " : fo << sol[i] << ' ';
  return 0;
}