10.24 test2 T2圖論+裴蜀定理
阿新 • • 發佈:2018-11-02
題幹
思路
裴蜀定理:n元線性方程有解當且僅當s是gcd(a,b,c……,z)的倍數
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const ll N=1e5+100; const ll M=1e6+100; ll read() { char ch=' '; ll f=1;ll x=0; while(ch<'0'||ch>'9') { if(ch=='-') f=-1;ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar(); } return x*f; } struct node { ll v,nxt; ll w; }edge[M]; ll head[N],cnt; void add(ll u,ll v,ll w) { cnt++; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].nxt=head[u]; head[u]=cnt; } ll dis[N]; void dfs(ll u,ll fa) { for(ll i=head[u];i;i=edge[i].nxt) { ll v=edge[i].v; ll w=edge[i].w; if(v!=fa) { dis[v]=dis[u]+w; dfs(v,u); } } } ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } int main() { ll n,q; n=read();q=read(); ll i,j; for(i=1;i<n;i++) { ll u,v,w; u=read();v=read();w=read(); add(u,v,w); add(v,u,-w); } dis[1]=0; dfs(1,0); ll hh=-1; ll gg=0; for(i=1;i<=q;i++) { ll op,x,y,k; op=read();x=read();y=read();k=read(); if(op==0) { ll tmp=dis[x]-dis[y]+k; tmp=abs(tmp); if(hh==-1) { hh=1; gg=tmp; } else { gg=gcd(gg,tmp); } } else if(op==1) { ll tmp=dis[y]-dis[x]; if(hh==-1) { if(tmp==k) { puts("yes"); } else { puts("no"); } } else { if(abs(tmp-k)%gg==0) puts("yes"); else puts("no"); } } } return 0; }