Can be done with DFS, but the precision will be a bit fried, after the various to take the logarithm or modulo meaning of the operation ...
But this problem can use a long double water past ... - -|
Code:
#include <bits/stdc++.h>using namespaceStd;inlineDouble_fabs (DoubleA) {returnA>0? a:-A;}#defineMAXN 1005#defineMAXM 10005Const Doubleeps=1e-9; inlineintRead () {intret=0, f=1;CharC=GetChar (); while(c>'9'|| c<'0') {if(c=='-') f=-1; c=GetChar ();} while(c<='9'&&c>='0') {ret=ret*Ten+c-'0'; c=GetChar ();} returnret*F;}intt,n,m;Long DoubleR[MAXN];BOOLVIS[MAXN];inthead[maxn],cnt;structedge{intTo,next,x,y;} E[MAXM*2];inlinevoidInsertintAintBintXinty) {e[++cnt].next=head[a];head[a]=cnt;e[cnt].to=b;e[cnt].x=x;e[cnt].y=y; e[++cnt].next=head[b];head[b]=cnt;e[cnt].to=a;e[cnt].x=y;e[cnt].y=x;}intA[MAXM],B[MAXM],X[MAXM],Y[MAXM];voidinit () {memset (head,0,sizeof(head)); Cnt=0; memset (Vis,0,sizeof(VIS)); Memset (R,0,sizeof(R));}voidDfsintv) {Vis[v]=1; for(intI=head[v];i;i=e[i].next)if(!Vis[e[i].to]) {R[e[i].to]=r[v]/e[i].x*e[i].y; DFS (E[I].TO); }}intMain () {T=read (); for(intp=1;p <=t;p++) {n=read (); m=read (); Init ();BOOLflag=0; for(intI=1; i<=m;i++) {A[i]=read (); B[i]=read (); X[i]=read (); y[i]=read (); Insert (A[i],b[i],x[i],y[i]); } for(intI=1; i<=n;i++)if(!vis[i]) r[i]=1, DFS (i); printf ("Case #%d:", p); for(intI=1; i<=m;i++) if(_fabs (R[a[i]]*y[i]-r[b[i]]*x[i]) >eps) {Puts ("No"); flag=1; Break;} if(!flag) puts ("Yes"); } return 0;}
bzoj4602--sdoi2016 Gears