洛谷 #1955. 程序自动分析

版权声明:转载请注明出处 https://blog.csdn.net/qq_41593522/article/details/84403035

题意

有n个表达式 != 或 =,判断是否矛盾

题解

离散化+并查集

调试记录

f数组开得太小

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100005

using namespace std;

int T, n, f[maxn << 2];

struct node{
	int x, y, opt;
}q[maxn];

int getf(int x){
	return (x == f[x]) ? x : (f[x] = getf(f[x]));
}

int arr[maxn << 2]; 

int main(){
	scanf("%d", &T);
	
	while (T--){
		memset(arr, 0, sizeof arr);
		scanf("%d", &n);
		
		for (int i = 1; i <= n; i++){
			scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].opt);
			arr[++arr[0]] = q[i].x, arr[++arr[0]] = q[i].y;
		}
		
		sort(arr + 1, arr + arr[0] + 1);
		
		int m = n;
		for (int i = 1; i <= n; i++){
			m = max(m, (q[i].x = lower_bound(arr + 1, arr + arr[0] + 1, q[i].x) - arr));
			m = max(m, (q[i].y = lower_bound(arr + 1, arr + arr[0] + 1, q[i].y) - arr));
		}
		
		for (int i = 1; i <= m; i++) f[i] = i;
		
		for (int i = 1; i <= n; i++)
			if (q[i].opt) 
				if (getf(q[i].x) != getf(q[i].y)) f[getf(q[i].x)] = getf(q[i].y);
		
		bool flag = false;
		for (int i = 1; i <= n; i++)
			if (!q[i].opt)
				if (getf(q[i].x) == getf(q[i].y)){
					printf("NO\n");
					flag = true;
					break;
				}
		if (!flag) printf("YES\n");
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41593522/article/details/84403035