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

#pragma region kyomukyomupurin

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-05-18 00:07:16
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

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

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  int n = 0;
  for (auto e : vec)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &st) {
  int n = 0;
  for (auto e : st)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) {
  int n = 0;
  for (auto e : mp)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  for (T &e : vec)
    is >> e;
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

#pragma endregion kyomukyomupurin

using graph = vector<vector<int>>;

using pii = pair<int, int>;

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

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

  vector<int> ans(n, -1);

  vector<bool> visited(n, false);
  visited[0] = true;

  queue<int> que;
  que.emplace(0);
  while (!que.empty()) {
    int cur = que.front();
    que.pop();
    for (int nxt : g[cur]) {
      if (visited[nxt])
        continue;
      ans[nxt] = cur;
      visited[nxt] = true;
      que.emplace(nxt);
    }
  }

  cout << "Yes" << '\\n';
  for (int i = 1; i < n; ++i) {
    cout << ans[i] + 1 << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

#pragma region kyomukyomupurin

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-05-18 00:07:16
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

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

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  int n = 0;
  for (auto e : vec)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &st) {
  int n = 0;
  for (auto e : st)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) {
  int n = 0;
  for (auto e : mp)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  for (T &e : vec)
    is >> e;
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

#pragma endregion kyomukyomupurin

using graph = vector<vector<int>>;

using pii = pair<int, int>;

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

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

  vector<int> ans(n, -1);

  vector<bool> visited(n, false);
  visited[0] = true;

  queue<int> que;
  que.emplace(0);
  while (!que.empty()) {
    int cur = que.front();
    que.pop();
    for (int nxt : g[cur]) {
      if (visited[nxt])
        continue;
      ans[nxt] = cur;
      visited[nxt] = true;
      que.emplace(nxt);
    }
  }

  cout << "Yes" << '\\n';
  for (int i = 1; i < n; ++i) {
    cout << ans[i] + 1 << '\\n';
  }

  return 0;
}