Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int gi() {
  int x = 0, w = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return w ? x : -x;
}
const int N = 1e6 + 5;
int n, m, q, mod, a[N], b[N], c[N], g, z, fa[N], chk[2][N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void link(int x, int y) { fa[find(x)] = find(y); }
int id(int u, int x, int y) { return u * 6 + x * 2 + y; }
int main() {
  n = gi();
  m = gi();
  q = gi();
  mod = gi();
  for (int i = 1; i <= m; ++i) {
    a[i] = gi(), b[i] = gi(), c[i] = gi();
    g = __gcd(g, c[i] - c[1]);
  }
  if (!g)
    g = mod;
  mod = __gcd(mod, 3 * g);
  z = c[1] % g;
  for (int i = 0; i < n * 6; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = a[i] - 1, v = b[i] - 1, w = (c[i] - z) / g % 3;
    for (int x = 0; x < 3; ++x) {
      link(id(u, x, 0), id(v, (2 * x + w) % 3, 1));
      link(id(u, x, 1), id(v, (2 * x + w) % 3, 0));
      link(id(v, x, 0), id(u, (2 * x + w) % 3, 1));
      link(id(v, x, 1), id(u, (2 * x + w) % 3, 0));
    }
  }
  for (int i = 0, j = z; i < mod << 1; ++i, j = (j << 1) % mod)
    chk[i & 1][j] = 1;
  while (q--) {
    int s = gi() - 1, t = gi() - 1, r = gi(), res = 0;
    for (int x = 0; x < 3; ++x)
      for (int y = 0; y < 2; ++y)
        if (find(id(t, 0, 0)) == find(id(s, x, y)))
          res |= chk[y][(r + z + (3 - x) * g) % mod];
    puts(res ? "YES" : "NO");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int gi() {
  int x = 0, w = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return w ? x : -x;
}
const int N = 1e6 + 5;
int n, m, q, mod, a[N], b[N], c[N], g, z, fa[N], chk[2][N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void link(int x, int y) { fa[find(x)] = find(y); }
int id(int u, int x, int y) { return u * 6 + x * 2 + y; }
int main() {
  n = gi();
  m = gi();
  q = gi();
  mod = gi();
  for (int i = 1; i <= m; ++i) {
    a[i] = gi(), b[i] = gi(), c[i] = gi();
    g = __gcd(g, abs(c[i] - c[1]));
  }
  if (!g)
    g = mod;
  mod = __gcd(mod, 3 * g);
  z = c[1] % g;
  for (int i = 0; i < n * 6; ++i)
    fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = a[i] - 1, v = b[i] - 1, w = (c[i] - z) / g % 3;
    for (int x = 0; x < 3; ++x) {
      link(id(u, x, 0), id(v, (2 * x + w) % 3, 1));
      link(id(u, x, 1), id(v, (2 * x + w) % 3, 0));
      link(id(v, x, 0), id(u, (2 * x + w) % 3, 1));
      link(id(v, x, 1), id(u, (2 * x + w) % 3, 0));
    }
  }
  for (int i = 0, j = z; i < mod << 1; ++i, j = (j << 1) % mod)
    chk[i & 1][j] = 1;
  while (q--) {
    int s = gi() - 1, t = gi() - 1, r = gi(), res = 0;
    for (int x = 0; x < 3; ++x)
      for (int y = 0; y < 2; ++y)
        if (find(id(t, 0, 0)) == find(id(s, x, y)))
          res |= chk[y][(r + z + (3 - x) * g) % mod];
    puts(res ? "YES" : "NO");
  }
  return 0;
}