Problem p02678 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

int gcd(int x, int y);

const int INF = 1001001001;

// mint�p�̕ϐ� (10��9��)
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> miti(m, vector<int>(0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    miti[a - 1].emplace_back(b - 1);
    miti[b - 1].emplace_back(a - 1);
  }
  vector<int> dist(n, INF);
  queue<int> room;
  vector<int> pre(n, -1);
  room.push(0);
  while (!room.empty()) {
    int num = room.front();
    room.pop();
    for (int r : miti[num]) {
      if (dist[r] != INF)
        continue;
      dist[r] = dist[num] + 1;
      pre[r] = num;
      room.push(r);
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << endl;
  }
  return 0;
}

/*���C�u�����ő����*/
// ���[�O���b�h�̌ݏ��@
int gcd(int x, int y) {
  int num[3];
  num[0] = (x > y) ? x : y;
  num[1] = (x <= y) ? x : y;
  num[2] = num[0] % num[1];

  while (num[2]) {
    num[0] = num[1];
    num[1] = num[2];
    num[2] = num[0] % num[1];
  }

  return num[1];
}


Fixed submission:
#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

int gcd(int x, int y);

const int INF = 1001001001;

// mint�p�̕ϐ� (10��9��)
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> miti(n, vector<int>(0));
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    miti[a - 1].emplace_back(b - 1);
    miti[b - 1].emplace_back(a - 1);
  }
  vector<int> dist(n, INF);
  queue<int> room;
  vector<int> pre(n, -1);
  room.push(0);
  while (!room.empty()) {
    int num = room.front();
    room.pop();
    for (int r : miti[num]) {
      if (dist[r] != INF)
        continue;
      dist[r] = dist[num] + 1;
      pre[r] = num;
      room.push(r);
    }
  }

  cout << "Yes" << endl;
  rep(i, n) {
    if (i == 0)
      continue;
    cout << pre[i] + 1 << endl;
  }
  return 0;
}

/*���C�u�����ő����*/
// ���[�O���b�h�̌ݏ��@
int gcd(int x, int y) {
  int num[3];
  num[0] = (x > y) ? x : y;
  num[1] = (x <= y) ? x : y;
  num[2] = num[0] % num[1];

  while (num[2]) {
    num[0] = num[1];
    num[1] = num[2];
    num[2] = num[0] % num[1];
  }

  return num[1];
}