First consider four lattice XOR values of 1.
Then (focus) found that the value of each lattice is only related to the top, leftmost, and (in) the color of the lattice.
The color of the enumeration (a), simultaneous equations, can reduce the unknowns, then the check set can be done.
At the end of the answer, some of the connected blocks are color-determined, some are uncertain, and are uncertain.
Pay attention to the details in this question! In fact, the first idea is the most difficult to think.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 1000500#defineMoD 1000000000using namespacestd;structpnt{intX,y,c;} P[MAXN];intn,m,k,fath[maxn<<1],dis[maxn<<1],val[maxn<<1],flag=-1,cnt[maxn<<1];voidReset () { for(intI=1; i<= (n1) + (M-1); i++) {Fath[i]=i; Dis[i]=0; Val[i]=-1; Cnt[i]=0; }}intGetfather (intx) { if(X==fath[x])returnFath[x]; if(val[x]!=-1) { if((val[fath[x]]!=-1) && (val[fath[x]]!= (val[x]^dis[x]))) return-1; VAL[FATH[X]]=val[x]^Dis[x]; } intret=Getfather (fath[x]); DIS[X]^=Dis[fath[x]]; FATH[X]=ret; returnfath[x];}intF_pow (intAintb) { int Base=a,ans=1; while(b) {if(b&1) ans= (ans*Base)%MoD; Base=(Base*Base)%MoD; b>>=1; } returnans%MoD;}intGetsintR) { intans=1; if(flag==1-R)return 0; Reset (); for(intI=1; i<=k;i++) { if((p[i].x==1) && (p[i].y==1)) { if(P[I].C!=R)return 0; } Else if((p[i].x==1) && (p[i].y!=1)) {if((val[n+p[i].y-2]!=P[I].C) && (val[n+p[i].y-2]!=-1))return 0; val[n+p[i].y-2]=p[i].c;} Else if((p[i].x!=1) && (p[i].y==1)) {if((val[p[i].x-1]!=P[I].C) && (val[p[i].x-1]!=-1))return 0; val[p[i].x-1]=p[i].c;} Else { intx=p[i].x-1, y=n+p[i].y-2; intF1=getfather (x), f2=Getfather (y); if((f1==-1) || (f2==-1))return 0; if(f1==F2) { intret=dis[x]^dis[y]^R; if((p[i].x%2==0) && (p[i].y%2==0)) ret^=1; if(RET!=P[I].C)return 0; } Else { intret=p[i].c^dis[x]^dis[y]^R; if((p[i].x%2==0) && (p[i].y%2==0)) ret^=1; if((val[f1]!=-1) && (val[f2]!=-1)) { if((Val[f1]^ret)!=VAL[F2])return 0; } Else if((val[f1]==-1) && (val[f2]!=-1)) {fath[f1]=f2;dis[f1]=ret;} Else if((val[f1]!=-1) && (val[f2]==-1)) {fath[f1]=f2;dis[f1]=ret;val[f2]=val[f1]^ret;} Else{fath[f1]=f2;dis[f1]=ret;} } } } for(intI=1; i<= (n1) + (M-1); i++) { intret=Getfather (i); if(ret==-1)return 0; Cnt[ret]++; } for(intI=1; i<= (n1) + (M-1); i++) { if((fath[i]==i) && (val[i]==-1)) Ans= (ans*2)%MoD; } returnans%MoD;}intMain () {scanf ("%d%d%d",&n,&m,&k); for(intI=1; i<=k;i++) {scanf ("%d%d%d",&p[i].x,&p[i].y,&p[i].c); if((p[i].x==1) && (p[i].y==1)) flag=p[i].c; } printf ("%d\n", (Gets (0) +gets (1))%MoD); return 0;}
Bzoj 2303 Square Staining