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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
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;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int nmax = 100005;
vector<vector<pair<ll, ll>>> g(nmax);
ll d[nmax];
void dijkstra(ll s) {
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  fill(d, d + nmax, INF);
  d[s] = 0;
  pq.push(make_pair(0, s));
  while (!pq.empty()) {
    pair<ll, ll> p = pq.top();
    pq.pop();
    ll v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (auto x : g[v]) {
      if (d[x.first] > d[v] + x.second) {
        d[x.first] = d[v] + x.second;
        pq.push(make_pair(d[x.first], x.first));
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m), c(n, inf), ans(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--, b[i]--;
    g[a[i]].push_back({b[i], 1});
    g[b[i]].push_back({a[i], 1});
  }
  dijkstra(0);
  rep(i, m) {
    if (d[a[i]] < d[b[i]]) {
      if (c[b[i]] > d[a[i]]) {
        c[b[i]] = d[a[i]];
        ans[b[i]] = a[i];
      }
    } else if (d[a[i]] > d[b[i]]) {
      if (c[a[i]] > d[b[i]]) {
        c[a[i]] = d[b[i]];
        ans[a[i]] = b[i];
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
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;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int nmax = 100005;
vector<vector<pair<ll, ll>>> g(nmax);
ll d[nmax];
void dijkstra(ll s) {
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  fill(d, d + nmax, INF);
  d[s] = 0;
  pq.push(make_pair(0, s));
  while (!pq.empty()) {
    pair<ll, ll> p = pq.top();
    pq.pop();
    ll v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    for (auto x : g[v]) {
      if (d[x.first] > d[v] + x.second) {
        d[x.first] = d[v] + x.second;
        pq.push(make_pair(d[x.first], x.first));
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n), c(n, inf), ans(n);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--, b[i]--;
    g[a[i]].push_back({b[i], 1});
    g[b[i]].push_back({a[i], 1});
  }
  dijkstra(0);
  rep(i, m) {
    if (d[a[i]] < d[b[i]]) {
      if (c[b[i]] > d[a[i]]) {
        c[b[i]] = d[a[i]];
        ans[b[i]] = a[i];
      }
    } else if (d[a[i]] > d[b[i]]) {
      if (c[a[i]] > d[b[i]]) {
        c[a[i]] = d[b[i]];
        ans[a[i]] = b[i];
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << ans[i] + 1 << endl;
  }
}