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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOOD 1000000007

using namespace std;
using ll = long long;
using P = pair<int, int>;

int cmpnum(const void *a, const void *b) {
  long long *A = (long long *)a;
  long long *B = (long long *)b;
  if (*A > *B)
    return 1;
  if (*A < *B)
    return -1;
  return 0;
}

int char_sort_max(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int char_sort_min(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int gcd(int x, int y) {
  int t;

  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}
ll lcm(ll a, ll b) { return (ll)((ll)(a * b) / (ll)(gcd(a, b))); }

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1){};

  int find(int x) {
    if (d[x] < 0) {
      return x;
    } else {
      return d[x] = find(d[x]);
    }
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);

    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

struct mint {
  ll x;

  mint(ll x) : x((x % MOOD + MOOD) % MOOD) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOOD - a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }

  mint &operator*=(const mint a) {
    if ((x *= a.x) %= MOOD)
      ;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(MOOD - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

mint kaijyo(int n) {
  if (n == 0)
    return 1;
  mint x = kaijyo(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

int shakutori(ll x, ll n, ll k, ll a[]) {
  long long int ai, res, ok, ng, mid;
  res = 0;
  for (int i = 0; i < n; i++) {
    // printf("%lld\\n", res);
    ai = a[i];
    if (ai == 0) {
      if (x < 0) {
        res += 0;
      } else {
        res += n - 1;
      }
    } else {
      if (ai <= -1) {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = n;
        ng = -1;
        while (ok - ng > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += n - ok;
      } else {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = -1;
        ng = n;
        while (ng - ok > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += ok + 1;
      }
    }
  }
  // printf("%lld\\n", res);
  return k <= res / 2;
}

int two_n(int tei, int jyousuu) {
  if (jyousuu == 0)
    return 1;
  int x = two_n(tei, jyousuu / 2);
  x *= x;
  if (jyousuu % 2 == 1) {
    x *= tei;
  }
  return x;
}
const double PI = 3.14159265358979323846;

int main() {
  int n, m;
  cin >> n;
  cin >> m;
  vector<vector<int>> d(n + 1);

  int mejirusi[n + 1];
  for (int i = 1; i <= n; ++i) {
    mejirusi[i] = -1;
  }

  queue<int> que;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    d[a].push_back(b);
    d[b].push_back(a);
  }
  for (int i = 0; i < d[1].size(); ++i) {
    que.push(d[1][i]);
    mejirusi[d[1][i]] = 1;
  }

  while (!que.empty()) {
    int A = que.front();
    for (int i = 0; i < d[A].size(); ++i) {
      if (mejirusi[d[A][i]] == -1 && d[A][i] != 1) {
        que.push(d[A][i]);
        mejirusi[d[A][i]] = A;
      }
    }
    que.pop();
  }

  for (int i = 2; i <= n; ++i) {
    if (mejirusi[i] == -1) {
      printf("No");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; ++i) {
    printf("%d\\n", mejirusi[i]);
  }

  return 0;
}

int A() {
  ll n, m;
  cin >> n;
  if (n % 10 == 2 || n % 10 == 4 || n % 10 == 5 || n % 10 == 7 || n % 10 == 9) {
    printf("hon");
  } else if (n % 10 == 0 || n % 10 == 1 || n % 10 == 6 || n % 10 == 8) {
    printf("pon");
  } else {
    printf("bon");
  }
}

int B() {

  int K, m;
  cin >> K;
  char S[101];
  scanf("%s", &S);
  if (K >= strlen(S)) {
    printf("%s", S);
  } else {
    for (int i = 0; i < K; ++i) {
      printf("%c", S[i]);
    }
    printf("...");
  }
}

int C() {
  double a, b;
  cin >> a >> b;
  double hour, second;
  cin >> hour >> second;
  double second_x, second_y;
  double hour_x, hour_y;
  hour_x = a * sin(2 * PI * (hour * 60 + second) / 720.000000000000000);
  hour_y = a * cos(2 * PI * (hour * 60 + second) / 720.000000000000000);
  second_x = b * sin(2 * PI * second / 60.00000000000000);
  second_y = b * cos(2 * PI * second / 60.00000000000000);

  double ans;
  ans = sqrt((hour_x - second_x) * (hour_x - second_x) +
             (hour_y - second_y) * (hour_y - second_y));
  printf("%.15lf", ans);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOOD 1000000007

using namespace std;
using ll = long long;
using P = pair<int, int>;

int cmpnum(const void *a, const void *b) {
  long long *A = (long long *)a;
  long long *B = (long long *)b;
  if (*A > *B)
    return 1;
  if (*A < *B)
    return -1;
  return 0;
}

int char_sort_max(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int char_sort_min(const void *a, const void *b) {
  return strcmp((char *)a, (char *)b);
}

int gcd(int x, int y) {
  int t;

  while (y != 0) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}
ll lcm(ll a, ll b) { return (ll)((ll)(a * b) / (ll)(gcd(a, b))); }

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1){};

  int find(int x) {
    if (d[x] < 0) {
      return x;
    } else {
      return d[x] = find(d[x]);
    }
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);

    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

struct mint {
  ll x;

  mint(ll x) : x((x % MOOD + MOOD) % MOOD) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOOD - a.x) >= MOOD)
      x -= MOOD;
    return *this;
  }

  mint &operator*=(const mint a) {
    if ((x *= a.x) %= MOOD)
      ;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(MOOD - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

mint kaijyo(int n) {
  if (n == 0)
    return 1;
  mint x = kaijyo(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

int shakutori(ll x, ll n, ll k, ll a[]) {
  long long int ai, res, ok, ng, mid;
  res = 0;
  for (int i = 0; i < n; i++) {
    // printf("%lld\\n", res);
    ai = a[i];
    if (ai == 0) {
      if (x < 0) {
        res += 0;
      } else {
        res += n - 1;
      }
    } else {
      if (ai <= -1) {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = n;
        ng = -1;
        while (ok - ng > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += n - ok;
      } else {
        if (ai * ai <= x) {
          res -= 1;
        }
        ok = -1;
        ng = n;
        while (ng - ok > 1) {
          mid = (ok + ng) / 2;
          if (a[mid] * ai <= x) {
            ok = mid;
          } else {
            ng = mid;
          }
        }
        res += ok + 1;
      }
    }
  }
  // printf("%lld\\n", res);
  return k <= res / 2;
}

int two_n(int tei, int jyousuu) {
  if (jyousuu == 0)
    return 1;
  int x = two_n(tei, jyousuu / 2);
  x *= x;
  if (jyousuu % 2 == 1) {
    x *= tei;
  }
  return x;
}
const double PI = 3.14159265358979323846;

int main() {
  int n, m;
  cin >> n;
  cin >> m;
  vector<vector<int>> d(n + 1, vector<int>(n + 1, 0));

  int mejirusi[n + 1];
  for (int i = 1; i <= n; ++i) {
    mejirusi[i] = -1;
  }

  queue<int> que;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    d[a][b] = 1;
    d[b][a] = 1;
  }
  for (int i = 2; i <= n; ++i) {
    if (d[1][i]) {
      que.push(i);
      mejirusi[i] = 1;
    }
  }

  while (!que.empty()) {
    int A = que.front();
    for (int i = 2; i <= n; ++i) {
      if (d[A][i] && mejirusi[i] == -1) {
        que.push(i);
        mejirusi[i] = A;
      }
    }
    que.pop();
  }

  for (int i = 2; i <= n; ++i) {
    if (mejirusi[i] == -1) {
      printf("No");
      return 0;
    }
  }
  printf("Yes\\n");
  for (int i = 2; i <= n; ++i) {
    printf("%d\\n", mejirusi[i]);
  }

  return 0;
}

int A() {
  ll n, m;
  cin >> n;
  if (n % 10 == 2 || n % 10 == 4 || n % 10 == 5 || n % 10 == 7 || n % 10 == 9) {
    printf("hon");
  } else if (n % 10 == 0 || n % 10 == 1 || n % 10 == 6 || n % 10 == 8) {
    printf("pon");
  } else {
    printf("bon");
  }
}

int B() {

  int K, m;
  cin >> K;
  char S[101];
  scanf("%s", &S);
  if (K >= strlen(S)) {
    printf("%s", S);
  } else {
    for (int i = 0; i < K; ++i) {
      printf("%c", S[i]);
    }
    printf("...");
  }
}

int C() {
  double a, b;
  cin >> a >> b;
  double hour, second;
  cin >> hour >> second;
  double second_x, second_y;
  double hour_x, hour_y;
  hour_x = a * sin(2 * PI * (hour * 60 + second) / 720.000000000000000);
  hour_y = a * cos(2 * PI * (hour * 60 + second) / 720.000000000000000);
  second_x = b * sin(2 * PI * second / 60.00000000000000);
  second_y = b * cos(2 * PI * second / 60.00000000000000);

  double ans;
  ans = sqrt((hour_x - second_x) * (hour_x - second_x) +
             (hour_y - second_y) * (hour_y - second_y));
  printf("%.15lf", ans);
}