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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// #include <bits/stdc++.h>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#include <time.h>
typedef std::pair<int, int> pii;
using namespace std;
using ll = long long;
#define PI 3.14159265359

int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> added;
  vector<vector<int>> g(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  queue<int> q;
  q.push(0);
  while (q.size()) {
    int sz = q.size();
    for (int i = 0; i < sz; i++) {
      int cur = q.front();
      q.pop();
      for (auto ne : g[cur]) {
        if (added.find(ne) == added.end() && ne != 0) {
          added[ne] = cur + 1;
          q.push(ne);
        }
      }
    }
  }
  if (added.size() != n - 1)
    cout << "No";
  else {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << added[i] << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// #include <bits/stdc++.h>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#include <time.h>
typedef std::pair<int, int> pii;
using namespace std;
using ll = long long;
#define PI 3.14159265359

int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> added;
  vector<vector<int>> g(n, vector<int>());
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  queue<int> q;
  q.push(0);
  while (q.size()) {
    int sz = q.size();
    for (int i = 0; i < sz; i++) {
      int cur = q.front();
      q.pop();
      for (auto ne : g[cur]) {

        if (added[ne] == 0 && ne != 0) {
          added[ne] = cur + 1;
          q.push(ne);
        }
      }
    }
  }
  if (added.size() != n)
    cout << "No";
  else {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << added[i] << endl;
    }
  }
}