Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T> void print(const T &v);

const int V = 10000;
vector<P> G[V]; // pair<辺の距離, 行き先の頂点> (隣接リスト)
ll dist[V];     // dist[i]はsから頂点iへの最短距離が入る
bool used[V];
void dijkstra(ll s) { // s: 始点
  fill_n(dist, V, INT_MAX);
  fill_n(used, V, false);
  priority_queue<P, vector<P>, greater<P>>
      q; // 値が小さい順に取り出されるpriority_queue
  q.push(P(0, s));
  while (!q.empty()) {
    ll d, t; // d: sからの距離 t: 行き先
    tie(d, t) = q.top();
    q.pop();
    if (used[t])
      continue; // 既に探索済か
    used[t] = true;
    dist[t] = d;
    for (P e : G[t]) {
      if (dist[e.second] <= d + e.first)
        continue;
      q.push(P(d + e.first, e.second));
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
  }
  rep(i, M) {
    G[a[i]].push_back(make_pair(1, b[i]));
    G[b[i]].push_back(make_pair(1, a[i]));
  }
  dijkstra(0);
  vector<int> ans(N);
  for (int i = 1; i < N; i++) {
    int next = dist[i] - 1; // 移動先の部屋の入り口までの距離
    for (auto p : G[i]) {
      if (dist[p.second] == next) {
        ans[i] = p.second;
        break;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << ans[i] + 1 << endl;
  }
  return 0;
}

// Use For Debug
template <typename T> void print(T const &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T> void print(const T &v);

const int V = 100100;
vector<P> G[V]; // pair<辺の距離, 行き先の頂点> (隣接リスト)
ll dist[V];     // dist[i]はsから頂点iへの最短距離が入る
bool used[V];
void dijkstra(ll s) { // s: 始点
  fill_n(dist, V, INT_MAX);
  fill_n(used, V, false);
  priority_queue<P, vector<P>, greater<P>>
      q; // 値が小さい順に取り出されるpriority_queue
  q.push(P(0, s));
  while (!q.empty()) {
    ll d, t; // d: sからの距離 t: 行き先
    tie(d, t) = q.top();
    q.pop();
    if (used[t])
      continue; // 既に探索済か
    used[t] = true;
    dist[t] = d;
    for (P e : G[t]) {
      if (dist[e.second] <= d + e.first)
        continue;
      q.push(P(d + e.first, e.second));
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M);
  rep(i, M) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
  }
  rep(i, M) {
    G[a[i]].push_back(make_pair(1, b[i]));
    G[b[i]].push_back(make_pair(1, a[i]));
  }
  dijkstra(0);
  vector<int> ans(N);
  for (int i = 1; i < N; i++) {
    int next = dist[i] - 1; // 移動先の部屋の入り口までの距離
    for (auto p : G[i]) {
      if (dist[p.second] == next) {
        ans[i] = p.second;
        break;
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << ans[i] + 1 << endl;
  }
  return 0;
}

// Use For Debug
template <typename T> void print(T const &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << " ";
    cout << v[i];
  }
  cout << endl;
}