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

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

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

const ll MOD = INF + 7;
int n, m;
Graph G(100100);
vector<int> flag(100100, -1);

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  queue<P> que;
  que.emplace(1, 1);

  while (!que.empty()) {
    int v = que.front().first, par = que.front().second;
    que.pop();
    // flag[v] = par;

    for (int nv : G[v]) {
      if (flag[nv] >= 0)
        continue;
      flag[nv] = v;
      que.emplace(nv, v);
    }
  }
  bool f = true;
  rep(i, n) if (flag[i + 1] == -1) f = false;
  if (!f)
    cout << "No";
  else {
    cout << "Yes" << endl;
    REP(i, 2, n) cout << flag[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

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

const ll MOD = INF + 7;
const ll MAX_V = 100100;

struct edge {
  int to;
  int cost;
};

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() {
  int E;
  cin >> V >> E;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge e1 = {b, 1};
    edge e2 = {a, 1};
    G[a].push_back(e1);
    G[b].push_back(e2);
  }
  dijkstra(0);
  bool f = true;
  REP(i, 1, E - 1) if (d[i] == INF) f = false;
  if (!f)
    cout << "No";
  else {
    cout << "Yes" << endl;
    REP(i, 1, E - 1) cout << d[i] << endl;
  }
}