Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
const int MAXP = 1e6 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int n, m, q, P, f[MAXN];
int tot, point[MAXN][2][3];
int x[MAXN], y[MAXN], w[MAXN];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
void merge(int x, int y) { f[find(x)] = find(y); }
bool same(int x, int y) { return find(x) == find(y); }
bool vis[2][MAXP];
int main() {
  read(n), read(m), read(q), read(P);
  int g = P;
  for (int i = 1; i <= m; i++) {
    read(x[i]), read(y[i]), read(w[i]);
    g = __gcd(g, w[i] - w[1]);
  }
  P = __gcd(3 * g, P);
  for (int i = 1; i <= n; i++)
    for (int p = 0; p <= 1; p++)
      for (int q = 0; q <= 2; q++) {
        point[i][p][q] = ++tot;
        f[tot] = tot;
      }
  int z = w[1] % g;
  for (int x = z, i = 0; i <= P; i++, x = 2 * x % P)
    vis[i & 1][x] = true;
  for (int i = 1; i <= m; i++) {
    int tx = x[i], ty = y[i];
    int tw = (w[i] - z) % P / g;
    for (int p = 0; p <= 1; p++)
      for (int q = 0; q <= 2; q++)
        merge(point[tx][p][q], point[ty][p ^ 1][(2 * q + tw) % 3]);
  }
  for (int i = 1; i <= q; i++) {
    bool res = false;
    int s, t, r;
    read(s), read(t), read(r);
    for (int p = 0; p <= 1; p++)
      for (int q = 0; q <= 2; q++)
        res |= same(point[t][0][0], point[s][p][q]) &&
               vis[p][(r + z + (3 - q) * g) % P];
    if (res)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
const int MAXP = 1e6 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int n, m, q, P, f[MAXN];
int tot, point[MAXN][2][3];
int x[MAXN], y[MAXN], w[MAXN];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
void merge(int x, int y) { f[find(x)] = find(y); }
bool same(int x, int y) { return find(x) == find(y); }
bool vis[2][MAXP];
int main() {
  read(n), read(m), read(q), read(P);
  int g = P;
  for (int i = 1; i <= m; i++) {
    read(x[i]), read(y[i]), read(w[i]);
    g = __gcd(g, abs(w[i] - w[1]));
  }
  P = __gcd(3 * g, P);
  for (int i = 1; i <= n; i++)
    for (int p = 0; p <= 1; p++)
      for (int q = 0; q <= 2; q++) {
        point[i][p][q] = ++tot;
        f[tot] = tot;
      }
  int z = w[1] % g;
  for (int x = z, i = 0; i <= P; i++, x = 2 * x % P)
    vis[i & 1][x] = true;
  for (int i = 1; i <= m; i++) {
    int tx = x[i], ty = y[i];
    int tw = (w[i] - z) % P / g;
    for (int p = 0; p <= 1; p++)
      for (int q = 0; q <= 2; q++)
        merge(point[tx][p][q], point[ty][p ^ 1][(2 * q + tw) % 3]);
  }
  for (int i = 1; i <= q; i++) {
    bool res = false;
    int s, t, r;
    read(s), read(t), read(r);
    for (int p = 0; p <= 1; p++)
      for (int q = 0; q <= 2; q++)
        res |= same(point[t][0][0], point[s][p][q]) &&
               vis[p][(r + z + (3 - q) * g) % P];
    if (res)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}