Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
// #define int long long
using P = pair<int, int>;

#define LOG(variable) cout << #variable ":\t" << (variable) << endl
#define LOGCON(i, container)                                                   \
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \
  cout << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, n, r) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
template <typename T = int> T in() {
  T a;
  cin >> a;
  return a;
}

int torch_nums[100005];
vector<vector<int>> paths(10005);
bool visited[100005];

void bfs() {
  queue<int> que;
  int current = 0;
  visited[0] = true;
  que.push(0);
  while (!que.empty()) {
    current = que.front();
    que.pop();
    for (auto &next : paths[current]) {
      if (visited[next])
        continue;
      que.push(next);
      torch_nums[next] = current;
      visited[next] = true;
    }
  }
}
/***** MAIN *****/
signed main() {
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    paths[a].PB(b);
    paths[b].PB(a);
  }

  bfs();
  bool flag = true;
  for (int i = 1; i < n; ++i) {
    if (!visited[i]) {
      flag = false;
      break;
    }
  }
  if (!flag) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << torch_nums[i] + 1 << endl;
  }
  return 0;
}
/***** MAIN *****/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
// #define int long long
using P = pair<int, int>;

#define LOG(variable) cout << #variable ":\t" << (variable) << endl
#define LOGCON(i, container)                                                   \
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \
  cout << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, n, r) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
template <typename T = int> T in() {
  T a;
  cin >> a;
  return a;
}

int torch_nums[100005];
vector<vector<int>> paths(200005);
bool visited[100005];

void bfs() {
  queue<int> que;
  int current = 0;
  visited[0] = true;
  que.push(0);
  while (!que.empty()) {
    current = que.front();
    que.pop();
    for (auto &next : paths[current]) {
      if (visited[next])
        continue;
      que.push(next);
      torch_nums[next] = current;
      visited[next] = true;
    }
  }
}
/***** MAIN *****/
signed main() {
  int n, m;
  cin >> n >> m;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    paths[a].PB(b);
    paths[b].PB(a);
  }

  bfs();
  bool flag = true;
  for (int i = 1; i < n; ++i) {
    if (!visited[i]) {
      flag = false;
      break;
    }
  }
  if (!flag) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; ++i) {
    cout << torch_nums[i] + 1 << endl;
  }
  return 0;
}
/***** MAIN *****/