bzoj2303 [apio2011 color] 分类: apio bzoj 2015-05-05 14:03 38人阅读 评论(0) 收藏
由题意,
只有当i, j全部为偶数时候,
其他情况,
所以,只要确定了第一行和第一列,整个矩阵就确定了。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
const int MAXN = 1e6+5, MAXP = 1e6+5, Mod = 1e9;
#define L(x) (x)
#define U(x) ((x)+n)
int n, m, p, pl, ans;
bool ff[2] = {true,true};
struct node{int x,y,t;}h[MAXP];
int fa[MAXN<<1], g[MAXN<<1], area;
int read()
{
int x = 0; char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0'), c = getchar();
return x;
}
void write(int x)
{
static char s[20];int sl = 0;
while(x) s[++sl] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[sl--]);
}
void Init()
{
n = read(), m = read(), p = read();
for(int i = 1; i <= p; i++)
h[i].x = read(),
h[i].y = read(),
h[i].t = read();
}
void Build()
{
for(int i = 1; i <= n+m; i++)
fa[i] = i, g[i] = 0;
fa[n + 1] = 1, area = n+m-1;
}
int Find(int x)
{
if (fa[x] == x) return x;
else
{
Find(fa[x]);
g[x] ^= g[fa[x]];
fa[x] = fa[fa[x]];
return fa[x];
}
}
int PowerMod(int a,int b)
{
int ret = 1;
while(b)
{
if(b&1) ret = (long long)ret * a % Mod;
a = (long long)a * a % Mod, b >>= 1;
}
return ret;
}
int GetAns(int st)
{
Build();
for(int i = 1,fl,fu,x,y,k; i <= p; i++)
{
// st^h[i].t^L(x)^U(y) = 0
k = st^h[i].t;
x = L(h[i].x), y = U(h[i].y);
fl = Find(x), fu = Find(y);
if(fl != fu)
{
fa[fu] = fl, g[fu] = g[x]^g[y]^k, area--;
}
else
{
if(g[x]^g[y]^k) return 0;
}
}
return PowerMod(2,area-1);
}
void Solve()
{
pl = 0;
for(int i = 1; i <= p; i++)
{
if(h[i].x == 1 && h[i].y == 1)
{ff[h[i].t^1] = false; continue;}
if(!(h[i].x&1) && !(h[i].y&1))
h[i].t ^= 1;
h[++pl] = h[i];
}
p = pl;
ans = 0;
if(ff[0]) ans = (ans + GetAns(0))%Mod;
if(ff[1]) ans = (ans + GetAns(1))%Mod;
return;
}
void Output()
{
write(ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("color.in","r",stdin);
freopen("color.out","w",stdout);
#endif
Init();
Solve();
Output();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}