Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
// #define INF 1LL<<60

#define INF 1e+9
#define MAX_V 10005

struct edge {
  int to;
  int cost;
};

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

int V;
vector<edge> G[MAX_V];
vector<int> par(MAX_V, -1);
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;
        par[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  V = N;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b;
    c = 1;
    a--;
    b--;
    edge e1 = {b, c}, e2 = {a, c};
    G[a].push_back(e1);
    G[b].push_back(e2);
  }
  par[0] = 0;
  dijkstra(0);

  bool flag = true;
  REP(i, N) if (par[i] == -1) flag = false;
  if (!flag) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    FOR(i, 1, N - 1) { cout << par[i] + 1 << endl; }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
// #define INF 1LL<<60

#define INF 1e+9
#define MAX_V 100005

struct edge {
  int to;
  int cost;
};

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

int V;
vector<edge> G[MAX_V];
vector<int> par(MAX_V, -1);
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;
        par[e.to] = v;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int N, M;
  cin >> N >> M;
  V = N;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b;
    c = 1;
    a--;
    b--;
    edge e1 = {b, c}, e2 = {a, c};
    G[a].push_back(e1);
    G[b].push_back(e2);
  }
  par[0] = 0;
  dijkstra(0);

  bool flag = true;
  REP(i, N) if (par[i] == -1) flag = false;
  if (!flag) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    FOR(i, 1, N - 1) { cout << par[i] + 1 << endl; }
  }
  return 0;
}