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

#include <bits/stdc++.h>
#include <cmath>

using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

#define ALL(obj) (obj).begin(), (obj).end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define VEC vector

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

typedef pair<int, int> P;
typedef vector<int> Vint;
typedef vector<Vint> VV;

template <typename T> string join(const v(T) & v) {
  stringstream s;
  REP(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> inline ostream &operator<<(ostream &o, const v(T) & v) {
  if (sz(v))
    o << join(v);
  return o;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define MAX 100010

const ll LINF = (ll)9e18 + 7; //
const int INF = 100100100;    // 2,147,483,647 (Max int)
const ll MOD = 1e9 + 7;
// テンプレ終了
//====================================================================//

// int DP[MAX];

v(Vint) E;
Vint cnt, pre;
queue<int> q;

void bfs() {
  if (q.empty())
    return;
  int n = q.front();
  q.pop();
  // cout << n << endl;
  for (int i : E[n]) {
    // cout << "\\t" << i << endl;
    if (cnt[i] <= cnt[n] + 1)
      continue;
    cnt[i] = cnt[n] + 1;
    pre[i] = n;
    q.push(i);
  }
  bfs();
}

int main() {
  int N, M;
  cin >> N >> M;
  E.resize(N);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    E[--a].push_back(--b);
    E[b].push_back(a);
  }

  cnt.resize(N, INF);
  pre.resize(N, 0);

  // cout << cnt << pre << endl;

  q.push(0);
  cnt[0] = 0;
  bfs();

  bool f = true;
  REPS(i, 1, N) if (cnt[i] == INF) {
    f = false;
    break;
  }
  if (f) {
    cout << "Yes" << endl;
    REPS(i, 1, N) cout << pre[i] + 1 << endl;
  } else
    cout << "No" << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>

using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

#define ALL(obj) (obj).begin(), (obj).end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define VEC vector

typedef unsigned int uint;
typedef unsigned long long int ull;
typedef long long int ll;

typedef pair<int, int> P;
typedef vector<int> Vint;
typedef vector<Vint> VV;

template <typename T> string join(const v(T) & v) {
  stringstream s;
  REP(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> inline ostream &operator<<(ostream &o, const v(T) & v) {
  if (sz(v))
    o << join(v);
  return o;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define MAX 100010

const ll LINF = (ll)9e18 + 7; //
const int INF = 100100100;    // 2,147,483,647 (Max int)
const ll MOD = 1e9 + 7;
// テンプレ終了
//====================================================================//

// int DP[MAX];

v(Vint) E;
Vint cnt, pre;
queue<int> q;

void bfs() {
  if (q.empty())
    return;
  int n = q.front();
  q.pop();
  // cout << n << endl;
  for (int i : E[n]) {
    // cout << "\\t" << i << endl;
    if (cnt[i] <= cnt[n] + 1)
      continue;
    cnt[i] = cnt[n] + 1;
    pre[i] = n;
    q.push(i);
  }
  bfs();
}

int main() {
  int N, M;
  cin >> N >> M;
  E.resize(M);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    E[--a].push_back(--b);
    E[b].push_back(a);
  }

  cnt.resize(N, INF);
  pre.resize(N, 0);

  q.push(0);
  cnt[0] = 0;
  bfs();

  bool f = true;
  REPS(i, 1, N) if (cnt[i] == INF) {
    f = false;
    break;
  }
  if (f) {
    cout << "Yes" << endl;
    REPS(i, 1, N) cout << pre[i] + 1 << endl;
  } else
    cout << "No" << endl;

  return 0;
}