Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 50005;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
vector<tuple<int, int, int>> edge;
int fa[6 * Maxn], chk[2][1000005];
int getid(int x, int p, int q) { return 6 * (x - 1) + 3 * p + q; }
int Getfa(int x) { return fa[x] = (fa[x] == x ? x : Getfa(fa[x])); }
int main() {
  int n, m, q, mod, g = 0;
  scanf("%d%d%d%d", &n, &m, &q, &mod);
  for (int i = 1; i <= 6 * n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    static int lst = -1;
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    edge.emplace_back(x, y, v);
    if (~lst)
      g = gcd(g, abs(v - lst));
    lst = v;
  }
  mod = gcd(mod, 3 * g);
  for (int i = 0; i < 2 * mod; i++) {
    static int j = get<2>(edge[0]) % g;
    chk[i & 1][j] = 1;
    j = 2 * j % mod;
  }
  for (auto &e : edge) {
    int x, y, v;
    tie(x, y, v) = e;
    for (int p = 0; p < 2; p++)
      for (int q = 0; q < 3; q++) {
        fa[Getfa(getid(x, p, q))] =
            Getfa(getid(y, p ^ 1, (2 * q + (v - v % g) / g) % 3));
        fa[Getfa(getid(y, p, q))] =
            Getfa(getid(x, p ^ 1, (2 * q + (v - v % g) / g) % 3));
      }
  }
  while (q--) {
    int x, y, v, succ = 0;
    scanf("%d%d%d", &x, &y, &v);
    for (int p = 0; p < 2 && !succ; p++)
      for (int q = 0; q < 3 && !succ; q++)
        if (Getfa(getid(y, 0, 0)) == Getfa(getid(x, p, q)))
          if (chk[p][(v + (3 - q) * g + get<2>(edge[0]) % g) % mod]) {
            succ = true;
            puts("YES");
            break;
          }
    if (!succ)
      puts("NO");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 50005;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
vector<tuple<int, int, int>> edge;
int fa[6 * Maxn], chk[2][1000005];
int getid(int x, int p, int q) { return 6 * (x - 1) + 3 * p + q; }
int Getfa(int x) { return fa[x] = (fa[x] == x ? x : Getfa(fa[x])); }
int main() {
  int n, m, q, mod, g = 0;
  scanf("%d%d%d%d", &n, &m, &q, &mod);
  for (int i = 1; i <= 6 * n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    static int lst = -1;
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    edge.emplace_back(x, y, v);
    if (~lst)
      g = gcd(g, abs(v - lst));
    lst = v;
  }
  if (g == 0)
    g = mod;
  mod = gcd(mod, 3 * g);
  for (int i = 0; i < 2 * mod; i++) {
    static int j = get<2>(edge[0]) % g;
    chk[i & 1][j] = 1;
    j = 2 * j % mod;
  }
  for (auto &e : edge) {
    int x, y, v;
    tie(x, y, v) = e;
    for (int p = 0; p < 2; p++)
      for (int q = 0; q < 3; q++) {
        fa[Getfa(getid(x, p, q))] =
            Getfa(getid(y, p ^ 1, (2 * q + (v - v % g) / g) % 3));
        fa[Getfa(getid(y, p, q))] =
            Getfa(getid(x, p ^ 1, (2 * q + (v - v % g) / g) % 3));
      }
  }
  while (q--) {
    int x, y, v, succ = 0;
    scanf("%d%d%d", &x, &y, &v);
    for (int p = 0; p < 2 && !succ; p++)
      for (int q = 0; q < 3 && !succ; q++)
        if (Getfa(getid(y, 0, 0)) == Getfa(getid(x, p, q)))
          if (chk[p][(v + (3 - q) * g + get<2>(edge[0]) % g) % mod]) {
            succ = true;
            puts("YES");
            break;
          }
    if (!succ)
      puts("NO");
  }
  return 0;
}