Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rng(i, l, r) for (size_t i = (l); i < (r); ++i)
#define rep(i, n) rng(i, 0, n)
#define gnr(i, l, r) for (size_t i = (r)-1; i >= (l); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(obj) (obj).begin(), (obj).end()    // 1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend() //...,3,2,1

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const double pi = acos(-1);
const int MOD = 1000000007;
const int INF = 1e18;
using Graph = vector<vector<int>>;
int n, m;
Graph G;
int seen[10010];
int path[10010];

void solve() {

  // remove the bottom 3 lines when you submit this code.
  std::ifstream in("sample.txt");
  std::cin.rdbuf(in.rdbuf());

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  G.resize(n);

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

  rep(i, 10010) {
    seen[i] = false;
    path[i] = INF;
  }

  queue<int> que;
  que.push(0);
  path[0] = 0;
  seen[0] = true;

  while (!que.empty()) {
    int now = que.front();
    que.pop();
    seen[now] = true;
    for (auto nv : G[now]) {
      if (seen[nv])
        continue;
      seen[nv] = true;
      path[nv] = now;
      que.push(nv);
    }
  }

  bool isPossible = true;
  rep(i, n) {
    if (seen[i] == false)
      isPossible = false;
  }

  if (isPossible) {
    cout << "Yes" << endl;
    rep(i, n - 1) cout << path[i + 1] + 1 << endl;
  } else {
    cout << "No" << endl;
  }
}

signed main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rng(i, l, r) for (size_t i = (l); i < (r); ++i)
#define rep(i, n) rng(i, 0, n)
#define gnr(i, l, r) for (size_t i = (r)-1; i >= (l); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(obj) (obj).begin(), (obj).end()    // 1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend() //...,3,2,1

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const double pi = acos(-1);
const int MOD = 1000000007;
const int INF = 1e18;
using Graph = vector<vector<int>>;
int n, m;
Graph G;
int seen[100100];
int path[100100];

void solve() {

  // remove the bottom 3 lines when you submit this code.
  std::ifstream in("sample.txt");
  std::cin.rdbuf(in.rdbuf());

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  G.resize(n);

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

  rep(i, 10010) {
    seen[i] = false;
    path[i] = INF;
  }

  queue<int> que;
  que.push(0);
  path[0] = 0;
  seen[0] = true;

  while (!que.empty()) {
    int now = que.front();
    que.pop();
    seen[now] = true;
    for (auto nv : G[now]) {
      if (seen[nv])
        continue;
      seen[nv] = true;
      path[nv] = now;
      que.push(nv);
    }
  }

  bool isPossible = true;
  rep(i, n) {
    if (seen[i] == false)
      isPossible = false;
  }

  if (isPossible) {
    cout << "Yes" << endl;
    rep(i, n - 1) cout << path[i + 1] + 1 << endl;
  } else {
    cout << "No" << endl;
  }
}

signed main() {
  solve();
  return 0;
}