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

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
const long long mod = 2e9 + 89;
const long long inf = 1e18;

/****** Tarjan’s SCC *******/
vector<int> num, low, S, vis, comp, adj[N];
int cntr, numCC;

void tarjanSCC(int v, int bef = -1) {
  low[v] = num[v] = ++cntr;
  vis[v] = 1;
  S.push_back(v);
  for (auto u : adj[v]) {
    if (u == bef)
      continue;
    if (num[u] == -1)
      tarjanSCC(u, v);
    if (vis[u])
      low[v] = min(low[v], low[u]);
  }
  if (low[v] == num[v]) {
    numCC++;
    while (1) {
      int u = S.back();
      S.pop_back();
      vis[u] = 0;
      comp[u] = numCC;
      if (u == v)
        break;
    }
  }
}

void dijkstra(int start, long long *d, vector<tuple<int, int, int>> edge[N]) {
  fill(d, d + N, inf);
  d[start] = 0;
  priority_queue<pair<long long, int>> pq;
  pq.emplace(-d[start], start);
  while (!pq.empty()) {
    long long cost;
    int now;
    tie(cost, now) = pq.top();
    pq.pop();
    cost = -cost;
    if (d[now] != cost)
      continue;
    for (auto it : edge[now]) {
      long long add;
      int near;
      tie(near, add, ignore) = it;
      long long ncos = cost + add;
      if (ncos < d[near]) {
        d[near] = ncos;
        pq.emplace(-d[near], near);
      }
    }
  }
}

long long d[N], drev[N];
int u[N], v[N], w[N];
vector<tuple<int, int, int>> edge[N]; // node - cost - id
vector<tuple<int, int, int>> rev[N];  // node - cost - id
bool good[N], done[N];

void dfs(int now) {
  if (done[now])
    return;
  done[now] = 1;
  for (auto it : rev[now]) {
    int to, cost, id;
    tie(to, cost, id) = it;
    if (d[to] + cost == d[now]) {
      good[id] = 1;
      dfs(to);
    }
  }
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", u + i, v + i, w + i);
    edge[u[i]].emplace_back(v[i], w[i], i);
    rev[v[i]].emplace_back(u[i], w[i], i);
  }
  dijkstra(1, d, edge);
  dijkstra(2, drev, rev);

  dfs(2);
  for (int i = 0; i < m; i++) {
    if (good[i]) {
      adj[u[i]].push_back(v[i]);
      adj[v[i]].push_back(u[i]);
    }
  }
  num.assign(n + 1, -1);
  low.assign(n + 1, 0);
  vis.assign(n + 1, 0);
  comp.assign(n + 1, -1);
  cntr = numCC = 0;
  for (int i = 1; i <= n; i++)
    if (num[i] == -1)
      tarjanSCC(i);

  /*
           puts("d");
           for (int i = 1; i <= n; i++) printf("%lld ", d[i]); printf("\\n");
           puts("good");
           for (int i = 0; i < m; i++) printf("%d ", good[i]); printf("\\n");
           puts("comp");
           for (int i = 1; i <= n; i++) printf("%d ", comp[i]); printf("\\n");
           */

  for (int i = 0; i < m; i++) {
    if (d[v[i]] + drev[u[i]] + w[i] < d[2]) {
      puts("HAPPY");
    } else if (good[i] && comp[u[i]] != comp[v[i]]) {
      puts("SAD");
    } else {
      puts("SOSO");
    }
  }

  return 0;
}


Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const double eps = 1e-6;
const double SQRT32 = 0.8660254037844386;
const double SQRT3 = 1.7320508075688772;
const double PI = 3.141592653589793;

int col[100][100];
int inv[10];
int f[10];

int check(double x, double goal) {
  if (x - goal > eps)
    return 1;
  if (goal - x > eps)
    return -1;
  return 0;
}

void print() {
  int p = 20;
  for (int i = 0; i < p; i++) {
    for (int j = i; j < p - 1; j++)
      printf("  ");
    for (int j = 0; j < i * 2 + 1; j++)
      printf("%d ", col[i][j]);
    printf("\\n");
  }
  return;
}

int init() {
  inv[1] = 3, inv[2] = 4, inv[3] = 1, inv[4] = 2;

  col[0][0] = 1;
  col[1][0] = 2;
  col[1][1] = 3;
  col[1][2] = 4;
  for (int i = 2; i < 36; i++) {
    int len = (i << 1) | 1;
    int mid = i;
    if (i & 1) {
      col[i][i] = inv[col[i - 1][i - 1]];
      col[i][i + 1] = col[i - 1][i];
    } else {
      col[i][i] = col[i - 1][i - 1];
      col[i][i + 1] = inv[col[i - 1][i]];
    }
    for (int j = i + 2; j < len; j += 2)
      col[i][j] = inv[col[i][j - 2]];
    for (int j = i - 2; j >= 0; j -= 2)
      col[i][j] = inv[col[i][j + 2]];
    for (int j = i + 3; j < len; j += 2)
      col[i][j] = inv[col[i][j - 2]];
    for (int j = i - 1; j >= 0; j -= 2)
      col[i][j] = inv[col[i][j + 2]];
  }
  // print();
  return 0;
}

int getans(int d, int l) {
  double x = l * sin((double)(d - 30) / 180.0 * PI),
         y = l * cos((double)(d - 30) / 180.0 * PI);
  // printf("x=%lf y=%lf\\n",x,y);
  int xx = 0;
  while (check((xx + 1) * SQRT32, y) < 0)
    xx++;

  int yy = 0;
  double dis;
  if (xx & 1)
    dis = (xx + 1) / 2.0 - y / SQRT3;
  else
    dis = y / SQRT3 - xx / 2.0;
  if (check(abs(x), dis) > 0) {
    double curx = dis;
    double tmpx = 1.0 - dis * 2;
    while (check(curx + tmpx, abs(x)) < 0) {
      yy++;
      curx += tmpx;
      tmpx = 1 - tmpx;
    }
  }

  if (check(x, 0) >= 0) {
    // printf("xx=%d yy=%d\\n",xx,yy);
    return col[xx][xx + yy];
  } else {
    // printf("xx=%d yy=%d\\n",xx,-yy);
    return col[xx][xx - yy];
  }
}

int ans[2];

int main() {

  init();
  char s[10];
  int d, l;
  for (int i = 0; i < 2; i++) {
    cin >> s >> d >> l;

    if (s[0] == 'B') {
      f[1] = 1;
      f[2] = 2;
      f[3] = 3;
      f[4] = 4;
    }
    if (s[0] == 'C') {
      f[1] = 4;
      f[2] = 1;
      f[3] = 3;
      f[4] = 2;
    }
    if (s[0] == 'D') {
      f[1] = 2;
      f[2] = 4;
      f[3] = 3;
      f[4] = 1;
    }
    ans[i] = f[getans(d, l)];
    // printf("ans[%d]=%d\\n",i,ans[i]);
  }

  if (ans[0] == ans[1])
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}