1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| const int N=2e5+10;
int n,m; struct Edge{ int to,nxt,w; }e[N]; int head[N],ecnt; void AddEdge(int u,int v,int w){ e[++ecnt]=(Edge){v,head[u],w}; head[u]=ecnt; }
ll G[N],S[N]; ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); } int t[N],low[N],dfn,stk[N],ins[N],top,id[N],scc; ll dis[N];
void dfs(int u){ ins[stk[++top]=u]=1,low[u]=t[u]=++dfn; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(!t[v]) { dis[v]=dis[u]+e[i].w; dfs(v),cmin(low[u],low[v]); } else if(ins[v]) { cmin(low[u],t[v]); G[u]=gcd(G[u],dis[u]-dis[v]+e[i].w); } } if(low[u]==t[u]) { ++scc; for(int v=-1;v!=u;) { ins[v=stk[top--]]=0; id[v]=scc,S[scc]=gcd(S[scc],G[v]); } } }
int main(){ n=rd(),m=rd(); rep(i,1,m) { int u=rd(),v=rd(),w=rd(); AddEdge(u,v,w); } rep(i,1,n) if(!t[i]) dfs(i); rep(_,1,rd()) { int u=rd(),s=rd(),t=rd(); s=gcd(s,t),t=gcd(t,S[id[u]]); puts(s%t==0?"YES":"NO"); } }
|