1. 程式人生 > >洛谷 #1955. 程式自動分析

洛谷 #1955. 程式自動分析

題意

有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; }