[bzoj2303][Apio2011] checkered staining


Sam and his sister Sara have one that contains n × A table of m squares. They want to dye each of their squares red or blue.
For personal preference, they want each 2 in the table × The square area of 2 contains an odd number (1 or 3) of red squares.
But last night, someone had dyed some squares in the form! Now Sam and Sara are very angry. However, they wanted to know if it was possible to color the remaining squares so that the whole form still met their requirements. If possible, how many dyeing schemes can meet their requirements?


The first row of the input contains three integers n,m and k, representing the number of rows, columns and stained squares of the table respectively.
The next k lines describe the stained squares. The i-th row contains three integers \ (x_i,y_i,c_i \), representing the row number, column number and color of the i-th stained square respectively\ A value of (c_i \) of 1 indicates that the square is dyed red, and a value of \ (c_i \) of 0 indicates that the square is dyed blue.


Output an integer representing the value obtained by the number of possible dyeing schemes W module \ (10 ^ 9 \).

Sample Input

3 4 3
2 2 1
1 2 0
2 3 1

Sample Output


\(2\leq n,m\leq10^6,0\leq k\leq10^6,1\leq x_i\leq n,1\leq y_i\leq m\).

Set red as 1 and blue as 0, and the problem constraint can be transformed into 2 for each requirement × The XOR sum of the square area of 2 is 1

If the first row has been determined, each subsequent row is either the odd column xor 1 of the previous row or the even column xor 1 of the previous row. Then the number of schemes is \ (\ large{2 ^ {empty rows} \)

Now we need to confirm whether there is such a legal first line, that is, whether the color XOR relationship between each two grids is contradictory

For each dyed lattice \ (x,y \) in row I, if their column numbers are the same as parity, their relationship in the first row is \ (c_x\;xor\;c_y \); If their column numbers are different from parity, it has made a total (i-1) transformation to the first row, and their relationship in the first row is \ (c_x\;xor\;c_y\;xor\;(i-1) \)
Then maintain the number of columns with the union query set, merge the dyed lattices of the same row, and record the XOR relationship with the father (it is convenient to O(1) find the XOR relationship between the lattices of the same connected block to judge the legitimacy). The number of schemes in the first row is \ (\ large{2 ^ {number of uncolored connected blocks in the first row} \)

#define N 1000005
#define M 1000000000
typedef long long ll;
struct grid{
	int x,y,c;
	bool friend operator <(grid a,grid b){
		if(a.x!=b.x) return a.x<b.x;
		return a.y<b.y;
int c[N]/*col[1][i]^col[1][f[i]]*/,f[N],n,m,k,tot;
bool b[N],v[N];
inline int gf(int k){
	if(f[k]==k) return k;
	int fa=gf(f[k]);
	return f[k]=fa;
inline bool chk(int x,int y,int t){
	int p=gf(x),q=gf(y);
		if(b[p]||b[q]) b[p]=b[q]=true;
		return true;
	else return (c[x]^c[y])==t;
inline int po(int x,int k){
	int ret=1;
		if(k&1) ret=1ll*ret*x%M;
	return ret;
inline void Aireen(){
	for(int i=1;i<=k;++i){
		if(a[i].x==1) b[a[i].y]=true;
	for(int i=1;i<=m;++i) f[i]=i;
	for(int i=2,x,y,t;i<=k;++i){
			if((x&1)^(y&1)) t^=((a[i].x-1)&1);
	for(int i=1;i<=m;++i)
		if(gf(i)==i&&!b[i]) ++tot;
	for(int i=2;i<=n;++i)
		if(!v[i]) ++tot;

2017-05-03 17:56:48

Tags: Union Find

Posted on Fri, 26 Nov 2021 00:58:35 -0500 by joebudden