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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod = 1e9 + 7;
/*"itob" int to "N"base */ template <typename TypeInt>
string itob(const TypeInt v, int base) {
  static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0)
      n *= -1;
  }
  while (n >= base) {
    ret += table[n % base];
    n /= base;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10)
      ret += '-';
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
/*"chmin" a = MAX*/ template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/*"chmax" a = MIN*/ template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*"ctoi"  char to int*/ int ctoi(char c) { return c - '0'; }
/*"gcd"   MAX Euclidean */ ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
/*"lcm"   MIN*/ ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*"primecheck"If prime,return true.*/ bool primecheck(ll n) {
  if (n < 2)
    return false;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return false;
    }
    return true;
  }
}
string reverserange(string s, ll a, ll b) {
  reverse(s.begin() + a - 1, s.begin() + b);
  return s;
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vvll gura(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    gura.at(a).push_back(b);
    gura.at(b).push_back(a);
  }
  vector<ll> ans(n, 1e9);
  queue<ll> que;
  que.push(0);
  vector<bool> seen(n, false);
  vector<ll> saitan(n, 1e9);
  saitan.at(0) = 0;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    if (seen.at(now))
      continue;
    seen.at(now) = true;
    for (ll i = 0; i < gura.at(now).size(); i++) {
      int next = gura.at(now).at(i);
      if (seen.at(next))
        continue;
      if (saitan.at(next) > saitan.at(now) + 1) {
        saitan.at(next) = saitan.at(now) + 1;
        ans.at(next) = now + 1;
      }
      que.push(next);
    }
  }
  bool che = true;
  for (int i = 1; i < n; i++) {
    if (ans.at(i) == 1e9) {
      che = false;
    }
  }
  if (che) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      cout << ans.at(i) << endl;
    }
    return 0;
  }
  cout << "No" << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod = 1e9 + 7;
/*"itob" int to "N"base */ template <typename TypeInt>
string itob(const TypeInt v, int base) {
  static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0)
      n *= -1;
  }
  while (n >= base) {
    ret += table[n % base];
    n /= base;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10)
      ret += '-';
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
/*"chmin" a = MAX*/ template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/*"chmax" a = MIN*/ template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*"ctoi"  char to int*/ int ctoi(char c) { return c - '0'; }
/*"gcd"   MAX Euclidean */ ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
/*"lcm"   MIN*/ ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*"primecheck"If prime,return true.*/ bool primecheck(ll n) {
  if (n < 2)
    return false;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return false;
    }
    return true;
  }
}
string reverserange(string s, ll a, ll b) {
  reverse(s.begin() + a - 1, s.begin() + b);
  return s;
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vvll gura(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    gura.at(a).push_back(b);
    gura.at(b).push_back(a);
  }
  vector<ll> ans(n, 1e9);
  queue<ll> que;
  que.push(0);
  vector<bool> seen(n, false);
  while (!que.empty()) {
    ll now = que.front();
    que.pop();
    if (seen.at(now))
      continue;
    seen.at(now) = true;
    for (int i = 0; i < gura.at(now).size(); i++) {
      ll next = gura.at(now).at(i);
      if (seen.at(next))
        continue;
      ans.at(next) = min(ans.at(next), now);
      que.push(next);
    }
  }
  bool che = true;
  for (int i = 1; i < n; i++) {
    if (ans.at(i) == 1e9) {
      che = false;
    }
  }
  if (che) {
    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) {
      ans.at(i)++;
      cout << ans.at(i) << endl;
    }
    return 0;
  }
  cout << "No" << endl;
}