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

//------------------------------------------------------------------------------MANAS
//UNIYAL---------------------------------------------------------
//                                                                              NEVER GIVE UP
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<ll, ll>
#define INF 0x3f3f3f3f
#define LINF 98765432123456789
#define testcases()                                                            \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define debug cout << "Hello\\n";
#define debug1(x) cout << #x << " " << x << "\\n";
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << "\\n";
#define debug3(x, y, z)                                                        \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << "\\n";
#define debug4(w, x, y, z)                                                     \\
  cout << #w << " " << w << " " << #x << " " << x << " " << #y << " " << y     \\
       << " " << #z << " " << z << "\\n";
#define rep(i, start, end) for (ll i = start; i < end; i++)
#define sz(x) (ll)((x).size())
#define all(x) (x).begin(), (x).end()
//----------------------------------------------------------------------------------------------------------------------------------------------------

vector<vector<int>> g;
vector<bool> visited;
vector<int> ans;
void bfs(int u = 1) {
  queue<pair<int, int>> q;
  q.push({u, 0});
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (auto it : g[p.fi]) {
      if (visited[it])
        continue;
      ans[it] = p.fi;
      q.push({it, p.se + 1});
      visited[it] = true;
    }
  }
}
int main() {
  fast;
  int n, m, u, v;
  cin >> n >> m;
  g.resize(n + 5);
  ans.resize(n + 5);
  visited.resize(n + 5, false);
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  bfs();
  bool ok = true;
  for (int i = 1; ok && i <= n; i++)
    if (!visited[i])
      ok = false;
  if (!ok)
    cout << "No";
  else {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++)
      cout << ans[i] << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

//------------------------------------------------------------------------------MANAS
//UNIYAL---------------------------------------------------------
//                                                                              NEVER GIVE UP
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<ll, ll>
#define INF 0x3f3f3f3f
#define LINF 98765432123456789
#define testcases()                                                            \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define debug cout << "Hello\\n";
#define debug1(x) cout << #x << " " << x << "\\n";
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << "\\n";
#define debug3(x, y, z)                                                        \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << "\\n";
#define debug4(w, x, y, z)                                                     \\
  cout << #w << " " << w << " " << #x << " " << x << " " << #y << " " << y     \\
       << " " << #z << " " << z << "\\n";
#define rep(i, start, end) for (ll i = start; i < end; i++)
#define sz(x) (ll)((x).size())
#define all(x) (x).begin(), (x).end()
//----------------------------------------------------------------------------------------------------------------------------------------------------

vector<vector<int>> g;
vector<bool> visited;
vector<int> ans;
void bfs(int u = 1) {
  queue<pair<int, int>> q;
  q.push({u, 0});
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (auto it : g[p.fi]) {
      if (visited[it])
        continue;
      ans[it] = p.fi;
      q.push({it, p.se + 1});
      visited[it] = true;
    }
  }
}
int main() {
  fast;
  int n, m, u, v;
  g.resize(n + 5);
  ans.resize(n + 5);
  visited.resize(n + 5, false);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  bfs();
  bool ok = true;
  for (int i = 1; ok && i <= n; i++)
    if (!visited[i])
      ok = false;
  if (!ok)
    cout << "No";
  else {
    cout << "Yes\\n";
    for (int i = 2; i <= n; i++)
      cout << ans[i] << "\\n";
  }
  return 0;
}