This problem is a DFS, there is a dairy problem almost the same. But the accuracy of this problem card.
There is a problem with another piece of the puzzle online. This method of taking the logarithm can be easily jammed. For example, 1e18 and (1e9-1) * (1e9+1) to take the logarithm is not guaranteed to not be card accuracy. So we need to change a method.
We take a large prime number, which is calculated in the modal sense of this prime number: multiplication is multiplication, division becomes multiply inverse, negative number plus a prime number. This approach may conflict, but is not data-independent.
#include <cstdio>using namespacestd;structEdge {intp; intA; intb; Edge*Next;} ; Const intp =10079 ;intPOW (intAintN) {intAns =1 ; while(n) {if(N &1) ans *= A, ans%=p; A*= A; A%=p; N/=2 ; } returnans;}intInt JConst inta) {returnPow (A, p-2 ) ; } Const intMAXN =1234 ; Const intMAXM =12345 ; intN, M; Edge E [Maxm*2 ] ; Edge*V [MAXN];BOOLvis [MAXN];intdis [MAXN];voidRead () {scanf ("%d%d", & N, &M); for(inti =1; I <= N; + + i) vis [i] = V [i] =0 ; while(M-- ) { intS, T, a, B; scanf ("%d%d%d%d", & S, & T, & A, &b); Edge*ConstT1= & E [M *2 ] ; T1p =T; T1-A =A; T1B = B <0? B +p:b; T1Next =V [s]; V [s]=T1; Edge*Constf = & E [M *2+1 ] ; Fp =s; FA = b <0? B +p:b; Fb =A; FNext =V [t]; V [T]=F; }}BOOLDFS (Const into) {Vis [O]=true ; for(Edge * v = v [o]; v; v = Vnext)if(! Vis [v-p]) {dis [vP] = dis [o] * v-a% P * INV (v-B)%p; if(Dfs (v-P))return false ; } Else if(Dis [v-p]! = DIS [o] * v-a% P * INV (v-B)%p)return false ; return true ;}BOOLsolve () { for(inti =1; I <= N; ++i)if(! Vis [i] && (dis [i] =1, ! DFS (i)))return false ; return true ;}intMain () {intT; scanf ("%d", &T); for(inti =1; I <= T; ++i) {read (); printf ("Case #%d:", i); Puts (solve ()?"Yes":"No" ) ; } return 0 ; }
BZOJ4602: [Sdoi2016] Gear DFS Inverse