70分求大佬帮忙
查看原帖
70分求大佬帮忙
590925
_x_y_楼主2022/8/2 14:19
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long mod = 1e9;
const int N = 100010;
int n, m, k;
int x[N], y[N], c[N];
int fa[N + N], d[N + N], col[N + N];
long long ans;
int get(int x){
    if(fa[x] == x)
		return x;
    d[x] = d[x] ^ d[fa[x]];
    return fa[x] = get(fa[x]);
}
long long work(){
    for(int i = 1; i <= n + m; i++){
    	fa[i] = i;
		d[i] = 0;
		col[i] = -1;
	}
	fa[n + 1] = 1;
	col[1] = 0;
    for(int i = 1; i <= k; i++){
    	int root1, root2, t;
        if(x[i] == 1 && y[i] == 1)
			continue;
        if(x[i] == 1 || y[i] == 1){
            if(y[i] == 1){
            	root1 = get(x[i]);
				t = x[i];
			}
            else{
            	root1 = get(y[i] + n);
				t = y[i] + n;
			}
            if(col[root1] == -1)
				col[root1] = c[i] ^ d[t];
            else if(col[root1] != c[i] ^ d[t])
				return 0;
            continue;
        }
        root1 = get(x[i]), root2 = get(y[i] + n);
        if(root1 != root2){
            fa[root1] = root2;
			d[root1] = (d[y[i] + n] - d[x[i]] + c[i] + 4) % 2;
            if((col[root1] != -1) && (col[root2] != -1) && (col[root2] != col[root1] ^ d[root1]))
				return 0;
            if(col[root2] == -1 && col[root1] != -1){
            	col[root2] = col[root1] ^ d[root1];
				col[root1] = -1;
			}
        }
        else if(d[x[i]] ^ d[y[i] + n] ^ c[i] == 1)
			return 0;
    }
    long long anss = 1;
    for(int i = 1; i <= n + m; i++)
		if(get(i) == i && col[i] == -1)
			anss = (anss * 2) % mod;
    return anss;
}
int main(){
    int flag = -1;
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= k; i++){
        scanf("%d%d%d", &x[i], &y[i], &c[i]);
        if(x[i] == 1 && y[i] == 1)
			flag = c[i];
        if(x[i] != 1 && y[i] != 1)
			c[i] = !c[i];
    }
    if(flag == -1 || flag == 0)
		ans += work();
    if(flag == -1 || flag == 1){
        for(int i = 1; i <= k; i++)
            if(x[i] != 1 && y[i] != 1)
				c[i] = !c[i];
        ans = (ans + work()) % mod;
    }
    printf("%lld", ans);
    return 0;
}
/*
3 4 3
2 2 1
1 2 0
2 3 1
*/
2022/8/2 14:19
加载中...