1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> using LL = long long; const int N = 1e5 + 100, P = 1e9; int n, m, num, nm, fa[N << 1], w[N << 1], rk[N << 1]; struct Q{ int x, y, c; } q[N]; int qpow(int x, int y) { int res = 1; for (; y; y >>= 1, x = LL(x) * x % P) if (y & 1) res = LL(res) * x % P; return res; } int get(int x) { if (x == fa[x]) return x; int t = get(fa[x]); w[x] ^= w[fa[x]]; return fa[x] = t; } bool merge(int x, int y, int v) { int fx = get(x), fy = get(y); v ^= w[x] ^ w[y]; if (fx == fy) return !v; if (rk[fx] > rk[fy]) std::swap(fx, fy); fa[fx] = fy, w[fx] = v, rk[fy] += rk[fx] == rk[fy]; return true; } int work(int col) { int res = 0; for (int i = 1; i <= nm; ++i) fa[i] = i, w[i] = 0; fa[n + 1] = 1; for (int i = 1, x, y, c; i <= num; ++i) { x = q[i].x, y = q[i].y, c = q[i].c; if (x == 1 && y == 1) if (c == col) continue; else return 0; c ^= !(x & 1) && !(y & 1); if (x > 1 && y > 1) c ^= col; if (!merge(x, y + n, c)) return 0; } for (int i = 1; i <= nm; ++i) res += get(i) == i; return qpow(2, res - 1); } int main() { scanf("%d %d %d", &n, &m, &num); nm = n + m; for (int i = 1; i <= num; ++i) scanf("%d %d %d", &q[i].x, &q[i].y, &q[i].c); printf("%d\n", (work(0) + work(1)) % P); return 0; }
|