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

#include <bits/stdc++.h>
#include <type_traits>
using namespace std;

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

vector<bool> visited;
vi ans;
vvi to;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  int n, m;
  std::cin >> n >> m;
  to.resize(n);
  visited.resize(n);
  ans.resize(n, -1);
  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    --a, --b;
    to[a].pb(b), to[b].pb(a);
  }
  vi que;
  que.pb(0);
  rep(i, que.size()) {
    for (auto v : to[que[i]]) {
      if (visited[v] || ans[v] != -1)
        continue;
      visited[v] = true;
      ans[v] = que[i];
      que.pb(v);
    }
  }
  bool ok = true;
  srep(i, 1, n) if (!visited[i]) ok = false;
  if (ok) {
    std::cout << "Yes"
              << "\\n";
    srep(i, 1, n) std::cout << ans[i] + 1 << "\\n";
  } else {
    std::cout << "No"
              << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <type_traits>
using namespace std;

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

vector<bool> visited;
vi ans;
vvi to;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  int n, m;
  std::cin >> n >> m;
  to.resize(n);
  visited.resize(n);
  ans.resize(n, -1);
  rep(i, n) {
    int a, b;
    std::cin >> a >> b;
    --a, --b;
    to[a].pb(b), to[b].pb(a);
  }
  vi que;
  que.pb(0);
  rep(i, que.size()) {
    for (auto v : to[que[i]]) {
      if (visited[v])
        continue;
      visited[v] = true;
      ans[v] = que[i];
      que.pb(v);
    }
  }
  bool ok = true;
  srep(i, 1, n) if (!visited[i]) ok = false;
  if (ok) {
    std::cout << "Yes"
              << "\\n";
    srep(i, 1, n) std::cout << ans[i] + 1 << "\\n";
  } else {
    std::cout << "No"
              << "\\n";
  }
}