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

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define rall(v) v.rbegin(), v.rend()
#define ones(x) __builtin_popcount(x)
#define sz(v)           ((int)((v).size())
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define clr(v, d) memset(v, d, sizeof(v));
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define rfo(i, n) for (ll i = n - 1; i >= 0; i--)
#define unq(vec)                                                               \\
  sort(all(vec));                                                              \\
  vec.resize(unique(all(vec)) - vec.begin());
#define era(v)                                                                 \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back
#define mp make_pair
#define endl '\\n'
#define fi first
#define se second
#define INF 1001001001

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
// ll modpow(ll a, ll b, ll m = MOD){a %= m; ll res = 1; while (b) {if (b &
// 1)res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;} ll bpow(ll a,
// ll b){ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;}
// return res;} ll modinv(ll a, ll m = MOD) {return modpow(a, m - 2, m);}

const ll MOD = 1e9 + 7;
const int dx4[4] = {0, 1, 0, -1};
const int dy4[4] = {-1, 0, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool cmp(int X, int Y) { return X > Y; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vi paren(n, -1);
  vi distanc(n, INF);
  vi graph[n];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].pb(b);
    graph[b].pb(a);
  }
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int w : graph[u]) {
      if (distanc[w] != INF) {
        continue;
      }
      paren[w] = u;
      distanc[w] = distanc[u] + 1;
      q.push(w);
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      continue;
    }
    int ans = paren[i];
    ans++;
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define rall(v) v.rbegin(), v.rend()
#define ones(x) __builtin_popcount(x)
#define sz(v)           ((int)((v).size())
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define clr(v, d) memset(v, d, sizeof(v));
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define rfo(i, n) for (ll i = n - 1; i >= 0; i--)
#define unq(vec)                                                               \\
  sort(all(vec));                                                              \\
  vec.resize(unique(all(vec)) - vec.begin());
#define era(v)                                                                 \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back
#define mp make_pair
#define endl '\\n'
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
// ll modpow(ll a, ll b, ll m = MOD){a %= m; ll res = 1; while (b) {if (b &
// 1)res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;} ll bpow(ll a,
// ll b){ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;}
// return res;} ll modinv(ll a, ll m = MOD) {return modpow(a, m - 2, m);}

const ll MOD = 1e9 + 7;
const int dx4[4] = {0, 1, 0, -1};
const int dy4[4] = {-1, 0, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
bool cmp(int X, int Y) { return X > Y; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, m;
  cin >> n >> m;
  vll parent(n, -1);
  vll distance(n, 1e9 + 5);
  vll graph[n];
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].pb(b);
    graph[b].pb(a);
  }
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll u = q.front();
    q.pop();
    for (auto w : graph[u]) {
      if (distance[w] != 1e9 + 5) {
        continue;
      }
      parent[w] = u;
      distance[w] = distance[u] + 1;
      q.push(w);
    }
  }
  cout << "Yes" << endl;
  for (ll i = 1; i < m; i++) {
    parent[i]++;
    cout << parent[i] << endl;
  }
}