Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int read() {
  int res = 0, w = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    c = getchar(), w = -1;
  while (isdigit(c))
    res = (res << 1) + (res << 3) + c - '0', c = getchar();
  return res * w;
}

struct Dsu {
  vector<int> fa;

  void init(int n) {
    fa.resize(n + 3);
    for (int i = 1; i <= n; i++)
      fa[i] = i;
  }

  int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

  bool merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return 0;
    fa[x] = y;
    return 1;
  }
} dsu;

const int N = 5e5 + 10;

int n, m, q, Mod;
int u[N], v[N], w[N];

int main() {
  n = read(), m = read(), q = read(), Mod = read();
  int g = Mod;
  for (int i = 1; i <= m; i++) {
    u[i] = read(), v[i] = read(), w[i] = read();
    if (i != 1) {
      g = __gcd(g, abs(w[i] - w[1]));
    }
  }
  if ((Mod / g) % 3 == 0)
    Mod = g * 3;
  else
    Mod = g;
  int z = w[1] % g;
  dsu.init((n + 2) * 6);
  for (int i = 1; i <= m; i++) {
    int p = (w[i] - z) / g % 3;
    for (int j = 0; j < 3; j++) {
      dsu.merge(u[i] * 6 + j * 2 + 0, v[i] * 6 + (2 * j + p) % 3 * 2 + 1);
      dsu.merge(u[i] * 6 + j * 2 + 1, v[i] * 6 + (2 * j + p) % 3 * 2 + 0);
      dsu.merge(v[i] * 6 + j * 2 + 0, u[i] * 6 + (2 * j + p) % 3 * 2 + 1);
      dsu.merge(v[i] * 6 + j * 2 + 1, u[i] * 6 + (2 * j + p) % 3 * 2 + 0);
    }
  }
  vector<vector<bool>> can(2, vector<bool>(g));
  for (int i = 0, j = z; i < g * 2; i++, j = j * 2 % Mod) {
    can[i & 1][j] = 1;
  }
  while (q--) {
    int s = read(), t = read(), r = read();
    bool ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 2; j++) {
        if (dsu.find(t * 6) == dsu.find(s * 6 + i * 2 + j)) {
          if (can[j][(r + z + (3 - i) * g) % Mod]) {
            ans = true;
          }
        }
      }
    }
    if (ans)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int read() {
  int res = 0, w = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    c = getchar(), w = -1;
  while (isdigit(c))
    res = (res << 1) + (res << 3) + c - '0', c = getchar();
  return res * w;
}

struct Dsu {
  vector<int> fa;

  void init(int n) {
    fa.resize(n + 3);
    for (int i = 1; i <= n; i++)
      fa[i] = i;
  }

  int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

  bool merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return 0;
    fa[x] = y;
    return 1;
  }
} dsu;

const int N = 5e5 + 10;

int n, m, q, Mod;
int u[N], v[N], w[N];

int main() {
  n = read(), m = read(), q = read(), Mod = read();
  int g = Mod;
  for (int i = 1; i <= m; i++) {
    u[i] = read(), v[i] = read(), w[i] = read();
    if (i != 1) {
      g = __gcd(g, abs(w[i] - w[1]));
    }
  }
  if ((Mod / g) % 3 == 0)
    Mod = g * 3;
  else
    Mod = g;
  int z = w[1] % g;
  dsu.init((n + 2) * 6);
  for (int i = 1; i <= m; i++) {
    int p = (w[i] - z) / g % 3;
    for (int j = 0; j < 3; j++) {
      dsu.merge(u[i] * 6 + j * 2 + 0, v[i] * 6 + (2 * j + p) % 3 * 2 + 1);
      dsu.merge(u[i] * 6 + j * 2 + 1, v[i] * 6 + (2 * j + p) % 3 * 2 + 0);
      dsu.merge(v[i] * 6 + j * 2 + 0, u[i] * 6 + (2 * j + p) % 3 * 2 + 1);
      dsu.merge(v[i] * 6 + j * 2 + 1, u[i] * 6 + (2 * j + p) % 3 * 2 + 0);
    }
  }
  vector<vector<bool>> can(2, vector<bool>(Mod));
  for (int i = 0, j = z; i < g * 2; i++, j = j * 2 % Mod) {
    can[i & 1][j] = 1;
  }
  while (q--) {
    int s = read(), t = read(), r = read();
    bool ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 2; j++) {
        if (dsu.find(t * 6) == dsu.find(s * 6 + i * 2 + j)) {
          if (can[j][(r + z + (3 - i) * g) % Mod]) {
            ans = true;
          }
        }
      }
    }
    if (ans)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}