Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
// #pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(x)                                                              \
  cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define debug2(x, y)                                                           \
  cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}"      \
       << endl;
#define debugv(v)                                                              \
  {                                                                            \
    cerr << #v << " : ";                                                       \
    for (auto x : v)                                                           \
      cerr << x << ' ';                                                        \
    cerr << endl;                                                              \
  }
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout << x << '\n', 0;

const int MAXN = 300010;

int n, m, q, u, v, x, y, t, r, mod;
int par[MAXN];
int A[MAXN], B[MAXN], C[MAXN], G, Z;
bool good[1000000];

int getpar(int x) { return (par[x] == x ? x : par[x] = getpar(par[x])); }
inline void join(int x, int y) { par[getpar(x)] = getpar(y); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  iota(par, par + MAXN, 0);
  cin >> n >> m >> q >> mod;
  for (int i = 1; i <= m; i++)
    cin >> A[i] >> B[i] >> C[i], G = __gcd(G, C[i] - C[1]);
  G = __gcd(G, mod);
  mod = __gcd(mod, 3 * G);
  Z = C[1] % G;
  for (int i = 1; i <= m; i++) {
    int u = A[i], v = B[i], c = C[i];
    for (int s : {0, 1, 2})
      for (int t : {0, 1}) {
        join(u * 6 + 2 * s + t, v * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1));
        join(v * 6 + 2 * s + t, u * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1));
      }
  }
  int val = Z;
  while (!good[val]) {
    good[val] = 1;
    val = val * 4 % mod;
  }

  while (q--) {
    cin >> u >> v >> r;
    bool ans = 0;
    for (int s : {0, 1, 2})
      for (int t : {0, 1})
        if (getpar(6 * v) == getpar(6 * u + 2 * s + t)) {
          int R = (r + Z - s * G) % mod;
          if (R < 0)
            R += mod;
          if (t)
            R = R * (mod + 1ll) / 2 % mod;
          ans |= good[R];
        }
    cout << (ans ? "YES\n" : "NO\n");
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
// #pragma GCC optimize("no-stack-protector,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(x)                                                              \
  cerr << #x << "= {" << (x.first) << ", " << (x.second) << "}" << endl;
#define debug2(x, y)                                                           \
  cerr << "{" << #x << ", " << #y << "} = {" << (x) << ", " << (y) << "}"      \
       << endl;
#define debugv(v)                                                              \
  {                                                                            \
    cerr << #v << " : ";                                                       \
    for (auto x : v)                                                           \
      cerr << x << ' ';                                                        \
    cerr << endl;                                                              \
  }
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout << x << '\n', 0;

const int MAXN = 300010;

int n, m, q, u, v, x, y, t, r, mod;
int par[MAXN];
int A[MAXN], B[MAXN], C[MAXN], G, Z;
bool good[1000000];

int getpar(int x) { return (par[x] == x ? x : par[x] = getpar(par[x])); }
inline void join(int x, int y) { par[getpar(x)] = getpar(y); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  iota(par, par + MAXN, 0);
  cin >> n >> m >> q >> mod;
  for (int i = 1; i <= m; i++)
    cin >> A[i] >> B[i] >> C[i], G = __gcd(G, C[i] - C[1]);
  G = __gcd(G, mod);
  if (G < 0)
    G *= -1;
  mod = __gcd(mod, 3 * G);
  Z = C[1] % G;
  for (int i = 1; i <= m; i++) {
    int u = A[i], v = B[i], c = C[i];
    for (int s : {0, 1, 2})
      for (int t : {0, 1}) {
        join(u * 6 + 2 * s + t, v * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1));
        join(v * 6 + 2 * s + t, u * 6 + 2 * ((2 * s + c / G) % 3) + (t ^ 1));
      }
  }
  int val = Z;
  while (!good[val]) {
    good[val] = 1;
    val = val * 4 % mod;
  }

  while (q--) {
    cin >> u >> v >> r;
    bool ans = 0;
    for (int s : {0, 1, 2})
      for (int t : {0, 1})
        if (getpar(6 * v) == getpar(6 * u + 2 * s + t)) {
          int R = (r + Z - s * G) % mod;
          if (R < 0)
            R += mod;
          if (t)
            R = R * (mod + 1ll) / 2 % mod;
          ans |= good[R];
        }
    cout << (ans ? "YES\n" : "NO\n");
  }

  return 0;
}