Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
int mod;
template <typename T = int> T gi() {
  T x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; };
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.fi) + "," + to_string(p.se) + ")";
}
template <typename T> string to_string(T v) {
  int fst = 1;
  string ret = "{";
  for (auto x : v) {
    if (!fst)
      ret += ",";
    fst = 0, ret += to_string(x);
  }
  ret += "}";
  return ret;
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  dbg_out(T...);
}
#define dbg(...) cerr << "{" << #__VA_ARGS__ << "}:", dbg_out(__VA_ARGS__)

template <typename T> int qpow(int a, T b) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = 1ll * ret * a % mod;
    a = 1ll * a * a % mod, b >>= 1;
  }
  return ret;
}

const int N = 1e6 + 10;

namespace dsu {

int fa[N];

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

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

void init(int n) {
  for (int i = 0; i < n; i++)
    fa[i] = i;
}

bool connected(int x, int y) { return getf(x) == getf(y); }

} // namespace dsu

int n, m, q, g = 0, z, a[N], b[N], c[N];
bool ispw[2][N];

int id(int u, int p, int q) { return 6 * u + 2 * q + p; }

int main() {
  n = gi(), m = gi(), q = gi(), mod = gi();
  for (int i = 1; i <= m; i++) {
    a[i] = gi() - 1, b[i] = gi() - 1, c[i] = gi();
    g = __gcd(g, c[i] - c[1]);
  }
  if (!g)
    g = mod;
  mod = __gcd(mod, 3 * g);
  z = c[1] % g;
  dsu::init(6 * n);
  for (int i = 1; i <= m; i++) {
    int w = (c[i] - z) / g % 3;
    for (int p = 0; p < 2; p++)
      for (int q = 0; q < 3; q++) {
        dsu::merge(id(a[i], p, q), id(b[i], p ^ 1, (2 * q + w) % 3));
        dsu::merge(id(b[i], p, q), id(a[i], p ^ 1, (2 * q + w) % 3));
      }
  }
  for (int i = 0, j = z % mod; !ispw[i & 1][j]; i++, j = 2ll * j % mod)
    ispw[i & 1][j] = 1;
  while (q--) {
    int s = gi() - 1, t = gi() - 1, r = gi();
    bool ans = 0;
    for (int p = 0; p < 2; p++)
      for (int q = 0; q < 3; q++) {
        if (dsu::connected(id(t, 0, 0), id(s, p, q))) {
          if (ispw[p & 1][(r + z - q * g + 3 * g) % mod])
            ans = 1;
        }
      }
    puts(ans ? "YES" : "NO");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
int mod;
template <typename T = int> T gi() {
  T x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; };
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.fi) + "," + to_string(p.se) + ")";
}
template <typename T> string to_string(T v) {
  int fst = 1;
  string ret = "{";
  for (auto x : v) {
    if (!fst)
      ret += ",";
    fst = 0, ret += to_string(x);
  }
  ret += "}";
  return ret;
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  dbg_out(T...);
}
#define dbg(...) cerr << "{" << #__VA_ARGS__ << "}:", dbg_out(__VA_ARGS__)

template <typename T> int qpow(int a, T b) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = 1ll * ret * a % mod;
    a = 1ll * a * a % mod, b >>= 1;
  }
  return ret;
}

const int N = 1e6 + 10;

namespace dsu {

int fa[N];

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

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

void init(int n) {
  for (int i = 0; i < n; i++)
    fa[i] = i;
}

bool connected(int x, int y) { return getf(x) == getf(y); }

} // namespace dsu

int n, m, q, g = 0, z, a[N], b[N], c[N];
bool ispw[2][N];

int id(int u, int p, int q) { return 6 * u + 2 * q + p; }

int main() {
  n = gi(), m = gi(), q = gi(), mod = gi();
  for (int i = 1; i <= m; i++) {
    a[i] = gi() - 1, b[i] = gi() - 1, c[i] = gi();
    g = __gcd(g, abs(c[i] - c[1]));
  }
  if (!g)
    g = mod;
  mod = __gcd(mod, 3 * g);
  z = c[1] % g;
  dsu::init(6 * n);
  for (int i = 1; i <= m; i++) {
    int w = (c[i] - z) / g % 3;
    for (int p = 0; p < 2; p++)
      for (int q = 0; q < 3; q++) {
        dsu::merge(id(a[i], p, q), id(b[i], p ^ 1, (2 * q + w) % 3));
        dsu::merge(id(b[i], p, q), id(a[i], p ^ 1, (2 * q + w) % 3));
      }
  }
  for (int i = 0, j = z % mod; !ispw[i & 1][j]; i++, j = 2ll * j % mod)
    ispw[i & 1][j] = 1;
  while (q--) {
    int s = gi() - 1, t = gi() - 1, r = gi();
    bool ans = 0;
    for (int p = 0; p < 2; p++)
      for (int q = 0; q < 3; q++) {
        if (dsu::connected(id(t, 0, 0), id(s, p, q))) {
          if (ispw[p & 1][(r + z - q * g + 3 * g) % mod])
            ans = 1;
        }
      }
    puts(ans ? "YES" : "NO");
  }
  return 0;
}