Problem p02678 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++i)
using namespace std;

using Graph = vector<vector<int>>;

int main() {
  int n, m;
  cin >> n >> m;
  Graph G(n, vector<int>());

  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> distance(n, -1);
  vector<int> previous(n, -1);
  queue<int> que;
  que.push(0);
  distance[0] = 0;

  while (!que.empty()) {
    int v = que.front();
    que.pop();

    for (auto u : G[v]) {
      if (distance[u] == -1) {
        distance[u] = distance[v] + 1;
        previous[u] = v;
        que.push(u);
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << previous[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

struct Edge {
  Edge(int _to, int _time) : to(_to), time(_time) {}
  int to;
  int time;
};
using Graph = vector<vector<Edge>>;
struct VInfo {
  VInfo(int _dist, int _num) : minimum_distance(_dist), v_num(_num) {}
  bool operator==(const VInfo &info) const {
    return this->minimum_distance == info.minimum_distance;
  }
  bool operator!=(const VInfo &info) const {
    return this->minimum_distance != info.minimum_distance;
  }
  bool operator<(const VInfo &info) const {
    return this->minimum_distance < info.minimum_distance;
  }
  bool operator<=(const VInfo &info) const {
    return this->minimum_distance <= info.minimum_distance;
  }
  bool operator>(const VInfo &info) const {
    return this->minimum_distance > info.minimum_distance;
  }
  bool operator>=(const VInfo &info) const {
    return this->minimum_distance >= info.minimum_distance;
  }
  int minimum_distance;
  int v_num;
};
void dijkstra(int s, vector<vector<Edge>> &graph, vector<int> &time,
              vector<int> &prev) {
  priority_queue<VInfo> q;
  time[s] = 0;
  q.emplace(0, s);
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int v = p.v_num;
    if (time[v] < p.minimum_distance)
      continue;
    for (int u = 0; u < graph[v].size(); u++) {
      if (time[graph[v][u].to] > time[v] + graph[v][u].time) {
        time[graph[v][u].to] = time[v] + graph[v][u].time;
        q.emplace(time[graph[v][u].to], graph[v][u].to);
        prev[graph[v][u].to] = v;
      }
    }
  }
};

vector<int> get_path(int t, vector<int> &prev) {
  vector<int> path;
  for (; t != -1; t = prev[t])
    path.push_back(t);

  return path;
}

int main() {
  int n, m;
  cin >> n >> m;

  Graph G(n, vector<Edge>());

  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v, 1);
    G[v].emplace_back(u, 1);
  }

  vector<int> time(n, INF);
  vector<int> prev(100, -1);

  dijkstra(0, G, time, prev);

  vector<int> sign(n, -1);

  for (int i = 1; i < n; ++i) {
    vector<int> path = get_path(i, prev);
    for (int j = 0; j < path.size() - 1; ++j) {
      if (sign[path[j]] > 0) {
        break;
      } else {
        sign[path[j]] = path[j + 1];
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << (sign[i] + 1) << endl;
  }
}