Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define MOD = 998244353;
#define int long long int
#define inf (int)(1e15)
#define all(x) (x).begin(), (x).end()
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define isYes(x) printf("%s\n", (x) ? "Yes" : "No")
#define isYES(x) printf("%s\n", (x) ? "YES" : "NO")
#define isIn(x, y, h, w) (x >= 0 && x < h && y >= 0 && y < w)
#define pair pair<int, int>
typedef vector<int> vi;   // Vector of long long
typedef vector<vi> vvi;   // Vector of vi
typedef vector<pair> vii; // Vector of pairs
typedef vector<vii> vvii; // Vector of Vector of pairs
typedef vector<bool> vb;  // Vector of bool
#define pq                                                                     \
  priority_queue // Max heap (To convert to min heap, use negative sign before
                 // every value)
#define ff first // For pairs
#define ss second
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, -1, 0, 1};
const int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1},
          dy8[] = {1, -1, 1, 0, -1, 1, -0, -1};
#define testcases(t)                                                           \
  int(t);                                                                      \
  cin >> (t);                                                                  \
  while ((t)--)
#define range(c, r) (c).begin(), (c).begin() + (r) // Mainly used in sorting
#define present(c, x) ((c).find(x) != (c).end())   // for sets, maps, multi-maps
#define cpresent(c, x) (find(all(c), x) != (c).end()) // for vectors
#define run(x, c) for ((x) = (c).begin(); (x) != (c).end(); (x)++)
vvi adj;
vi parent;
vb visited;
void bfs() {
  queue<int> q;
  q.push(1);
  visited[1] = true;
  while (q.size()) {
    int current = q.front();
    q.pop();
    for (auto it : adj[current]) {
      if (parent[it] == -1) {
        q.push(it);
        parent[it] = current;
        visited[it] = true;
      }
    }
  }
}
signed main() {
  int n, m;
  cin >> n >> m;
  adj.resize(n + 1);
  parent.assign(n + 1, -1);
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    if (visited[i])
      continue;
    cout << "No" << endl;
    return 0;
  }
  Yes;
  for (int i = 2; i <= n; i++) {
    cout << parent[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define MOD = 998244353;
#define int long long int
#define inf (int)(1e15)
#define all(x) (x).begin(), (x).end()
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define isYes(x) printf("%s\n", (x) ? "Yes" : "No")
#define isYES(x) printf("%s\n", (x) ? "YES" : "NO")
#define isIn(x, y, h, w) (x >= 0 && x < h && y >= 0 && y < w)
#define pair pair<int, int>
typedef vector<int> vi;   // Vector of long long
typedef vector<vi> vvi;   // Vector of vi
typedef vector<pair> vii; // Vector of pairs
typedef vector<vii> vvii; // Vector of Vector of pairs
typedef vector<bool> vb;  // Vector of bool
#define pq                                                                     \
  priority_queue // Max heap (To convert to min heap, use negative sign before
                 // every value)
#define ff first // For pairs
#define ss second
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, -1, 0, 1};
const int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1},
          dy8[] = {1, -1, 1, 0, -1, 1, -0, -1};
#define testcases(t)                                                           \
  int(t);                                                                      \
  cin >> (t);                                                                  \
  while ((t)--)
#define range(c, r) (c).begin(), (c).begin() + (r) // Mainly used in sorting
#define present(c, x) ((c).find(x) != (c).end())   // for sets, maps, multi-maps
#define cpresent(c, x) (find(all(c), x) != (c).end()) // for vectors
#define run(x, c) for ((x) = (c).begin(); (x) != (c).end(); (x)++)
vvi adj;
vi parent;
vb visited;
void bfs() {
  queue<int> q;
  q.push(1);
  visited[1] = true;
  while (q.size()) {
    int current = q.front();
    q.pop();
    for (auto it : adj[current]) {
      if (parent[it] == -1) {
        q.push(it);
        parent[it] = current;
        visited[it] = true;
      }
    }
  }
}
signed main() {
  int n, m;
  cin >> n >> m;
  adj.resize(n + 1);
  parent.assign(n + 1, -1);
  visited.assign(n + 1, false);
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    if (visited[i])
      continue;
    cout << "No" << endl;
    return 0;
  }
  Yes;
  for (int i = 2; i <= n; i++) {
    cout << parent[i] << endl;
  }
  return 0;
}