Problem p03100 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define N (500010)
#define M ()
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand()) << (15)) ^ (rand()))
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1 {
inline char read() {
  static const int IN_LEN = 1000000;
  static char buf[IN_LEN], *s, *t;
  return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),
          (s == t ? -1 : *s++) : *s++);
}
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = read(); !isdigit(c); c = read()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = read())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
inline char readc(char &c) {
  for (c = read(); !isalpha(c) && !isdigit(c); c = read())
    if (c == -1)
      return 0;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {
  if (ooh == obuf + OUT_LEN)
    fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
  *ooh++ = c;
}
template <class T> inline void print(T x) {
  static int buf[30], cnt;
  if (x == 0)
    print('0');
  else {
    if (x < 0)
      print('-'), x = -x;
    for (cnt = 0; x; x /= 10)
      buf[++cnt] = x % 10 + 48;
    while (cnt)
      print((char)buf[cnt--]);
  }
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
} // namespace fastIO1
namespace fastIO2 {
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = getchar())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
} // namespace fastIO2
using namespace fastIO1;
struct edge {
  int x, y, c;
} e[N];
int n, m, g, P, Q, fa[N], pos[N][2][3], res, tot;
bool can[1000010][2];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int ask(int x) { return fa[x] == x ? x : fa[x] = ask(fa[x]); }
void unite(int x, int y) {
  x = ask(x), y = ask(y);
  if (x != y)
    fa[x] = y;
}
int main() {
  freopen("01.txt", "r", stdin);
  freopen("my.out", "w", stdout);
  read(n), read(m), read(Q), read(P), g = P;
  for (int i = 1; i <= m; i++)
    read(e[i].x), read(e[i].y), read(e[i].c),
        g = gcd(g, (int)abs(e[i].c - e[1].c));
  //	cout<<g<<endl;
  P = gcd(P, 3 * g), res = e[1].c % g;
  for (int i = 1; i <= n * 6; i++)
    fa[i] = i;
  for (int i = 0, x = res; i <= P; i++, x = (x + x) % P)
    can[x][i & 1] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 2; k++)
        pos[i][j][k] = ++tot;
  for (int i = 1; i <= m; i++) {
    int w = (e[i].c - res) % P / g, x = e[i].x, y = e[i].y;
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 2; k++)
        unite(pos[x][j][k], pos[y][j ^ 1][(2 * k + w) % 3]);
  }
  while (Q--) {
    int x, y, z;
    bool fl = 0;
    read(x), read(y), read(z);
    for (int i = 0; i <= 1; i++)
      for (int j = 0; j <= 2; j++)
        fl |= (ask(pos[y][0][0]) == ask(pos[x][i][j])) &&
              can[(z + res + (3 - j) * g) % P][i];
    puts(fl ? "YES" : "NO");
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define N (500010)
#define M ()
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand()) << (15)) ^ (rand()))
#define file(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1 {
inline char read() {
  static const int IN_LEN = 1000000;
  static char buf[IN_LEN], *s, *t;
  return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),
          (s == t ? -1 : *s++) : *s++);
}
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = read(); !isdigit(c); c = read()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = read())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
inline char readc(char &c) {
  for (c = read(); !isalpha(c) && !isdigit(c); c = read())
    if (c == -1)
      return 0;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {
  if (ooh == obuf + OUT_LEN)
    fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
  *ooh++ = c;
}
template <class T> inline void print(T x) {
  static int buf[30], cnt;
  if (x == 0)
    print('0');
  else {
    if (x < 0)
      print('-'), x = -x;
    for (cnt = 0; x; x /= 10)
      buf[++cnt] = x % 10 + 48;
    while (cnt)
      print((char)buf[cnt--]);
  }
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
} // namespace fastIO1
namespace fastIO2 {
template <class T> inline void read(T &x) {
  static bool iosig;
  static char c;
  for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
    if (c == '-')
      iosig = true;
    if (c == -1)
      return;
  }
  for (x = 0; isdigit(c); c = getchar())
    x = ((x + (x << 2)) << 1) + (c ^ '0');
  if (iosig)
    x = -x;
}
} // namespace fastIO2
using namespace fastIO1;
struct edge {
  int x, y, c;
} e[N];
int n, m, g, P, Q, fa[N], pos[N][2][3], res, tot;
bool can[1000010][2];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int ask(int x) { return fa[x] == x ? x : fa[x] = ask(fa[x]); }
void unite(int x, int y) {
  x = ask(x), y = ask(y);
  if (x != y)
    fa[x] = y;
}
int main() {
  //	freopen("01.txt","r",stdin);
  //	freopen("my.out","w",stdout);
  read(n), read(m), read(Q), read(P), g = P;
  for (int i = 1; i <= m; i++)
    read(e[i].x), read(e[i].y), read(e[i].c),
        g = gcd(g, (int)abs(e[i].c - e[1].c));
  //	cout<<g<<endl;
  P = gcd(P, 3 * g), res = e[1].c % g;
  for (int i = 1; i <= n * 6; i++)
    fa[i] = i;
  for (int i = 0, x = res; i <= P; i++, x = (x + x) % P)
    can[x][i & 1] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 2; k++)
        pos[i][j][k] = ++tot;
  for (int i = 1; i <= m; i++) {
    int w = (e[i].c - res) % P / g, x = e[i].x, y = e[i].y;
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 2; k++)
        unite(pos[x][j][k], pos[y][j ^ 1][(2 * k + w) % 3]);
  }
  while (Q--) {
    int x, y, z;
    bool fl = 0;
    read(x), read(y), read(z);
    for (int i = 0; i <= 1; i++)
      for (int j = 0; j <= 2; j++)
        fl |= (ask(pos[y][0][0]) == ask(pos[x][i][j])) &&
              can[(z + res + (3 - j) * g) % P][i];
    puts(fl ? "YES" : "NO");
  }
}