Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define M 50010
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  int fh = 1;
  while (!isdigit(c)) {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  x *= fh;
}
struct edge {
  int x, y, z;
} a[M];
int m, n, q, mod, g, bi[1000010], num, f[M * 6], z, x, y, r;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
int qf(int x) {
  if (f[x] != x)
    f[x] = qf(f[x]);
  return f[x];
}
void lian(int x, int y) {
  int nx = qf(x), ny = qf(y);
  if (nx != ny)
    f[nx] = ny;
}
int que(int x, int y, int z) { return (y * 3 + z) * n + x; }
int main() {
  read(n);
  read(m);
  read(q);
  read(mod);
  for (int i = 1; i <= m; i++) {
    read(a[i].x);
    read(a[i].y);
    read(a[i].z);
    g = gcd(g, a[i].z - a[1].z);
  }
  if (!g)
    g = mod;
  mod = gcd(g * 3, mod);
  z = a[1].z % g;
  for (int i = 0; i < mod; i++)
    bi[i] = 0;
  for (int i = 0; i < mod; i++)
    if (!bi[i]) {
      num++;
      for (int j = i; !bi[j]; j = j * 4 % mod)
        bi[j] = num;
    }
  for (int i = 1; i <= m; i++)
    a[i].z -= z;
  for (int i = 1; i <= n * 6; i++)
    f[i] = i;
  for (int i = 1; i <= m; i++)
    for (int j = 0; j <= 1; j++)
      for (int p = 0; p <= 2; p++) {
        lian(que(a[i].x, j, p), que(a[i].y, j ^ 1, (p * 2 + a[i].z / g) % 3));
        lian(que(a[i].y, j, p), que(a[i].x, j ^ 1, (p * 2 + a[i].z / g) % 3));
      }
  for (int i = 1; i <= q; i++) {
    read(x);
    read(y);
    read(r);
    bool vi = 0;
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 2; k++)
        if (qf(que(y, 1, 0)) == qf(que(x, j, k)) &&
            bi[(r + z) % mod] == bi[((2 - j) * z + k * g) % mod])
          vi = 1;
    if (vi)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define M 50010
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  int fh = 1;
  while (!isdigit(c)) {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  x *= fh;
}
struct edge {
  int x, y, z;
} a[M];
int m, n, q, mod, g, bi[1000010], num, f[M * 6], z, x, y, r;
int gcd(int x, int y) {
  if (y == 0)
    return x;
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
int qf(int x) {
  if (f[x] != x)
    f[x] = qf(f[x]);
  return f[x];
}
void lian(int x, int y) {
  int nx = qf(x), ny = qf(y);
  if (nx != ny)
    f[nx] = ny;
}
int que(int x, int y, int z) { return (y * 3 + z) * n + x; }
int main() {
  read(n);
  read(m);
  read(q);
  read(mod);
  for (int i = 1; i <= m; i++) {
    read(a[i].x);
    read(a[i].y);
    read(a[i].z);
    g = gcd(g, abs(a[i].z - a[1].z));
  }
  if (!g)
    g = mod;
  mod = gcd(g * 3, mod);
  z = a[1].z % g;
  for (int i = 0; i < mod; i++)
    bi[i] = 0;
  for (int i = 0; i < mod; i++)
    if (!bi[i]) {
      num++;
      for (int j = i; !bi[j]; j = j * 4 % mod)
        bi[j] = num;
    }
  for (int i = 1; i <= m; i++)
    a[i].z -= z;
  for (int i = 1; i <= n * 6; i++)
    f[i] = i;
  for (int i = 1; i <= m; i++)
    for (int j = 0; j <= 1; j++)
      for (int p = 0; p <= 2; p++) {
        lian(que(a[i].x, j, p), que(a[i].y, j ^ 1, (p * 2 + a[i].z / g) % 3));
        lian(que(a[i].y, j, p), que(a[i].x, j ^ 1, (p * 2 + a[i].z / g) % 3));
      }
  for (int i = 1; i <= q; i++) {
    read(x);
    read(y);
    read(r);
    bool vi = 0;
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 2; k++)
        if (qf(que(y, 1, 0)) == qf(que(x, j, k)) &&
            bi[(r + z) % mod] == bi[((2 - j) * z + k * g) % mod])
          vi = 1;
    if (vi)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}