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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define repi(i, start, end) for (int64_t(i) = (start); (i) < (end); ++(i))
#define rep(i, end) repi((i), 0, (end))
#define rfor(x, v) for (auto &(x) : (v))
#define iterate(itr, v)                                                        \\
  for (auto(itr) = (v).begin(); (itr) != (v).end(); ++(itr))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define vin(v) rep(i, (v).size()) cin >> (v)[i];
#define vpin(v) rep(i, (v).size()) cin >> (v)[i].first >> (v)[i].second

using namespace std;
using Int = int64_t;
using pint = pair<Int, Int>;
using vint = vector<Int>;
using vvint = vector<vint>;
using vpint = vector<pint>;
using vstr = vector<string>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void put_double(T a) {
  cout << setprecision(12);
  cout << a << "\\n";
}
template <class T> void println(T a) { cout << a << "\\n"; }
template <class T> void put_line(T &v) {
  iterate(itr, v) { cout << *itr << ((itr == --v.end()) ? "\\n" : " "); }
}
// auto [x, y] = make_tuple(3, 4); => let (x, y) = (3, 4);
#define INF 1e+9
#define MAX_V 10

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<vector<edge>> G;
vint d;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;
  V = n;
  d.resize(n);
  rep(i, n) d[i] = LLONG_MAX;
  G.resize(n);
  vector<P> ab(m);
  vpin(ab);
  vvint connect(n);
  rfor(p, ab) {
    auto [x, y] = p;
    x--;
    y--;
    G[x].push_back({y, 1});
    G[y].push_back({x, 1});
  }
  dijkstra(0);
  vint ans(n, -1);
  repi(i, 1, n) {
    int cost = d[i];
    cost--;
    rfor(c, G[i]) {
      // cout << c.to + 1 << " ";
      if (d[c.to] == cost)
        ans[i] = c.to + 1;
    }
    // println("");
  }
  // put_line(ans);
  int flag = 1;
  repi(i, 1, n) flag &= ans[i] != -1;
  if (flag) {
    println("Yes");
    repi(i, 1, n) println(ans[i]);
  } else {
    println("No");
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define repi(i, start, end) for (int64_t(i) = (start); (i) < (end); ++(i))
#define rep(i, end) repi((i), 0, (end))
#define rfor(x, v) for (auto &(x) : (v))
#define iterate(itr, v)                                                        \\
  for (auto(itr) = (v).begin(); (itr) != (v).end(); ++(itr))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define vin(v) rep(i, (v).size()) cin >> (v)[i];
#define vpin(v) rep(i, (v).size()) cin >> (v)[i].first >> (v)[i].second

using namespace std;
using Int = int64_t;
using pint = pair<Int, Int>;
using vint = vector<Int>;
using vvint = vector<vint>;
using vpint = vector<pint>;
using vstr = vector<string>;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void put_double(T a) {
  cout << setprecision(12);
  cout << a << "\\n";
}
template <class T> void println(T a) { cout << a << "\\n"; }
template <class T> void put_line(T &v) {
  iterate(itr, v) { cout << *itr << ((itr == --v.end()) ? "\\n" : " "); }
}
// auto [x, y] = make_tuple(3, 4); => let (x, y) = (3, 4);
#define INF 1e+9
#define MAX_V 10

struct edge {
  int to;
  int cost;
};

// <最短距離, 頂点の番号>
using P = pair<int, int>;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main(void) {
  int n, m;
  cin >> n >> m;
  V = n;
  vector<P> ab(m);
  vpin(ab);
  vvint connect(n);
  rfor(p, ab) {
    auto [x, y] = p;
    x--;
    y--;
    G[x].push_back({y, 1});
    G[y].push_back({x, 1});
  }
  dijkstra(0);
  vint ans(n, -1);
  map<int64_t, vint> diff;
  rep(i, n) diff[d[i]].push_back(i);
  repi(i, 1, n) {
    int cost = d[i];
    cost--;
    rfor(c, G[i]) {
      // cout << c.to + 1 << " ";
      if (d[c.to] == cost)
        ans[i] = c.to + 1;
    }
    // println("");
  }
  // put_line(ans);
  int flag = 1;
  repi(i, 1, n) flag &= ans[i] != -1;
  if (flag) {
    println("Yes");
    repi(i, 1, n) println(ans[i]);
  } else {
    println("No");
  }

  return 0;
}