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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

#ifndef ONLINE_JUDGE
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " {" << x.first << " : " << x.second << "}"
      << ",";
  o << " }";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "{";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "}";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "{" << obj.first << ", " << obj.second << "}";
  return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
void print(void) { cout << endl; }
template <class Head> void print(Head &&head) {
  cout << head;
  print();
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print(forward<Tail>(tail)...);
}
#else
#define print(...)
#endif

void bfs(queue<pair<ll, ll>> &q, vector<list<ll>> &g, vector<ll> &flag) {
  queue<pair<ll, ll>> next_q;
  while (!q.empty()) {
    ll f = q.front().first;
    ll x = q.front().second;
    q.pop();
    print("#", f, ">", x);
    for (auto &&y : g[x]) {
      print("$", x, ">", y);
      if (flag[y] > flag.size()) {
        flag[y] = x;
        next_q.push(pair<ll, ll>(x, y));
      }
    }
  }
  if (!next_q.empty())
    bfs(next_q, g, flag);
  print(flag);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, m;

  cin >> n >> m;

  vector<list<ll>> g(n, list<ll>());
  vector<ll> flag(n, n + 1);
  flag[0] = 0;

  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;

    a--;
    b--;

    g[a].push_back(b);
    g[b].push_back(a);
  }

  queue<pair<ll, ll>> q;
  for (auto &&x : g[0]) {
    q.push(pair<ll, ll>(0, x));
    flag[x] = 0;
  }

  bfs(q, g, flag);

  cout << "Yes" << endl;

  for (int i = 1; i < n; i++)
    cout << flag[i] + 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

#ifndef ONLINE_JUDGE
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto &x : obj)
    o << " {" << x.first << " : " << x.second << "}"
      << ",";
  o << " }";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "{";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "}";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "{" << obj.first << ", " << obj.second << "}";
  return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
void print(void) { cout << endl; }
template <class Head> void print(Head &&head) {
  cout << head;
  print();
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head << " ";
  print(forward<Tail>(tail)...);
}
#else
#define print(...)
#endif

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, m;

  cin >> n >> m;

  vector<vector<ll>> g(n, vector<ll>(n, MOD));
  vector<ll> flag(n, n);

  for (int i = 0; i < n; i++) {
    g[i][i] = 0;
  }
  for (int i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;

    a--;
    b--;

    g[a][b] = 1;
    g[b][a] = 1;
  }

  ll max_len = 0;

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        g[i][j] = min(g[i][k] + g[k][j], g[i][j]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (g[i][j] != MOD)
        max_len = max(max_len, g[i][j]);
    }
  }

  for (int i = 1; i <= max_len; i++) {
    for (int j = 1; j < n; j++) {
      if (g[0][j] == i) {
        for (int k = 0; k < n; k++) {
          if (g[0][k] == i - 1 && g[j][k] == 1) {
            flag[j] = k;
            break;
          }
        }
        // print("#",i,flag[j]);
      }
    }
  }

  cout << "Yes" << endl;

  for (int i = 1; i < n; i++)
    cout << flag[i] + 1 << endl;

  return 0;
}