Language: C++

#include<bits/stdc++.h>
#define re register
unsigned int gcd(re unsigned int a,re unsigned int b)
{
	return b?gcd(b,a%b):a;
}
int exgcd(re int a,re int b,re int&x,re int&y)
{
	if(!b){x=1;y=0;return a;}
	re int g=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return g;
}
int x[100100],y[100100],c[100100],c1[100100],fa[600100],ff[1001000],ccc;
int find(re int a)
{
	return a==fa[a]?a:fa[a]=find(fa[a]);
}
void meg(re int a,re int b)
{
	a=find(a);b=find(b);fa[a]=b;
}
bool qry(re int x,re int y)
{
	return find(x)==find(y);
}
int main()
{
	re int n,m,q,mod,x1,y1,z,z1,g=0;
	scanf("%d%d%d%d",&n,&m,&q,&mod);
	for(re int i=1;i<=n;i++)c1[i]=-1;
	for(re int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x[i],&y[i],&c[i]);
		if(c1[x[i]]!=-1)g=gcd(g,abs(c[i]-c1[x[i]]));
		if(c1[y[i]]!=-1)g=gcd(g,abs(c[i]-c1[y[i]]));
		c1[x[i]]=c1[y[i]]=c[i];
	}
	for(re int i=1;i<=6*n;i++)fa[i]=i;
	mod=gcd(mod,3*g);
	if(!g)g=mod;
	z=c[1]%g;
	for(re int i=1;i<=m;i++)
	{
		c[i]-=z;
		re int cc=c[i]%(3*g)/g;
		for(re int i1=0;i1<=1;i1++)
			for(re int j1=0;j1<=2;j1++)meg(x[i]*6-3*i1-j1,y[i]*6-3*(i1^1)-(2*j1+cc)%3);
	}
	for(re int i=0;i<mod;i++)ff[i]=-1;
	re int xx=2%mod,yy=1;
	if(mod!=1)
	{
		ff[1]=0;
		while(xx!=1)ff[xx]=yy,xx=xx*2%mod,yy^=1;
	}
	for(;q--;)
	{
		scanf("%d%d%d",&x1,&y1,&z1);
		re bool bbl=0;
		z1=(z+z1)%mod;//printf("*****%d %d %d\n",z,z1,g);
		for(re int i=0;i<=2;i++)
		{
			re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
			if(z==0)
			{
				if(zz==0)
				{
					if(qry(6*y1,6*x1-i)||qry(6*y1,6*x1-i-3))
					{
						bbl=1;puts("YES");break;
					}
				}
				continue;
			}
			zk/=gg;zz/=gg;md/=gg;
			gg=exgcd(zk,md,xc,yc);
			xc=(xc+md)%md;
			xc=1ll*xc*zz%md;
			if(gg!=1||ff[xc]==-1)continue;
			assert(1ll*xc*xk%md==zz);
			if(qry(6*y1,6*x1-i-3*ff[xc])||qry(6*y1,6*x1-i-3*(ff[xc]^yy)))
			{
				bbl=1;puts("YES");break;
			}
		}
		if(!bbl)puts("NO");
	}
}

Compiler output:
a.cc:3:34: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    3 | unsigned int gcd(re unsigned int a,re unsigned int b)
      |                                  ^
a.cc:3:52: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    3 | unsigned int gcd(re unsigned int a,re unsigned int b)
      |                                                    ^
a.cc:7:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    7 | int exgcd(re int a,re int b,re int&x,re int&y)
      |                  ^
a.cc:7:27: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    7 | int exgcd(re int a,re int b,re int&x,re int&y)
      |                           ^
a.cc:7:36: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    7 | int exgcd(re int a,re int b,re int&x,re int&y)
      |                                    ^
a.cc:7:45: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    7 | int exgcd(re int a,re int b,re int&x,re int&y)
      |                                             ^
a.cc: In function 'int exgcd(int, int, int&, int&)':
a.cc:10:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   10 |         re int g=exgcd(b,a%b,y,x);
      |                ^
a.cc: At global scope:
a.cc:15:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   15 | int find(re int a)
      |                 ^
a.cc:19:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   19 | void meg(re int a,re int b)
      |                 ^
a.cc:19:26: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   19 | void meg(re int a,re int b)
      |                          ^
a.cc:23:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   23 | bool qry(re int x,re int y)
      |                 ^
a.cc:23:26: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   23 | bool qry(re int x,re int y)
      |                          ^
a.cc: In function 'int main()':
a.cc:29:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                ^
a.cc:29:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                  ^
a.cc:29:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                    ^
a.cc:29:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                      ^~~
a.cc:29:26: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                          ^~
a.cc:29:29: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                             ^~
a.cc:29:32: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                                ^
a.cc:29:34: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                                  ^~
a.cc:29:37: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   29 |         re int n,m,q,mod,x1,y1,z,z1,g=0;
      |                                     ^
a.cc:31:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   31 |         for(re int i=1;i<=n;i++)c1[i]=-1;
      |                    ^
a.cc:32:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   32 |         for(re int i=1;i<=m;i++)
      |                    ^
a.cc:39:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   39 |         for(re int i=1;i<=6*n;i++)fa[i]=i;
      |                    ^
a.cc:43:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   43 |         for(re int i=1;i<=m;i++)
      |                    ^
a.cc:46:24: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   46 |                 re int cc=c[i]%(3*g)/g;
      |                        ^~
a.cc:47:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 |                 for(re int i1=0;i1<=1;i1++)
      |                            ^~
a.cc:48:36: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   48 |                         for(re int j1=0;j1<=2;j1++)meg(x[i]*6-3*i1-j1,y[i]*6-3*(i1^1)-(2*j1+cc)%3);
      |                                    ^~
a.cc:50:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   50 |         for(re int i=0;i<mod;i++)ff[i]=-1;
      |                    ^
a.cc:51:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   51 |         re int xx=2%mod,yy=1;
      |                ^~
a.cc:51:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   51 |         re int xx=2%mod,yy=1;
      |                         ^~
a.cc:60:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   60 |                 re bool bbl=0;
      |                         ^~~
a.cc:62:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   62 |                 for(re int i=0;i<=2;i++)
      |                            ^
a.cc:64:32: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   64 |                         re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
      |                                ^~
a.cc:64:37: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   64 |                         re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
      |                                     ^~
a.cc:64:61: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   64 |                         re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
      |                                                             ^~
a.cc:64:68: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   64 |                         re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
      |                                                                    ^~
a.cc:64:90: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   64 |                         re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
      |                                                                                          ^~
a.cc:64:93: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   64 |                         re int zk=z,zz=(z1-i*g%mod+mod)%mod,md=mod,gg=gcd(zk,gcd(zz,md)),xc,yc;
      |                                                                                             ^~
In file included from /usr/include/c++/14/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:106,
                 from a.cc:1:
a.cc:81:39: error: 'xk' was not declared in this scope; did you mean 'xc'?
   81 |                         assert(1ll*xc*xk%md==zz);
      |                                       ^~