AcWing237. Program automatic analysis

AcWing237. Program automatic analysis

Ideas

First merge the equals and then judge whether the inequality contradicts the known conditions


In the process of automatic program analysis, it is often necessary to determine whether some constraints can be satisfied at the same time.

Consider a simplified version of the constraint satisfaction problem: suppose x 1, x 2, x 3 x_{1},x_{2},x_{3}x1,x2,x3,...Represent the variables appearing in the program, given n of the form xi = xj x_{i}=x_{j}xi=xjOr xi ≠ xj x_{i}≠x_{j}xi=xjFor the equal/unequal constraint conditions of the variables, please determine whether you can assign appropriate values ​​to each variable so that all the above constraints are met at the same time.

For example, the constraints in a problem are: x 1 = x 2, x 2 = x 3, x 3 = x 4, x 1 ≠ x 4 x_{1}=x_{2}, x_{2}=x_{ 3}, x_{3}=x_{4}, x_{1}≠x_{4}x1=x2x2=x3x3=x4x1=x4Obviously, these constraints cannot be satisfied at the same time, so this question should be judged as unsatisfiable.

Now give some constraint satisfaction problems, please judge them separately.

Input format

The first line of the input file contains a positive integer t, which represents the number of questions to be determined. Note that these questions are independent of each other.

For each question, contain several lines:

The first line contains a positive integer n, which represents the number of constraints that need to be met in the problem.

In the next n lines, each line includes 3 integers i, j, e, describing an equal/unequal constraint condition, separated by a single space between adjacent integers. If e=1, the constraint condition is xi = xj x_{i}=x_{j}xi=xj; If e=0, the constraint condition is xi ≠ xj x_{i}≠x_{j}xi=xj

Output format

The output file includes t lines.

The kth line of the output file outputs a character string "YES" or "NO" (without quotation marks, all uppercase letters), "YES" means that the kth question in the input is judged to be satisfied, and "NO" means that it cannot be satisfied Satisfy.

data range

1 ≤ n ≤ 1000000 1≤n≤1000000 1n1000000
1 ≤ i , j ≤ 1000000000 1≤i,j≤1000000000 1i,j1000000000

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
inline int gcd(int a, int b) {
    
     return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
    
     return x & -x; }


using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 2000010;
int f[N], n, m;
unordered_map<int, int>S;


struct Query {
    
    
	int x, y, e;
}query[N];
int Find(int x) {
    
    
	return x == f[x] ? x : f[x] = Find(f[x]);
}

int get(int x) {
    
    
	if (S.count(x) == 0)S[x] = ++n;
	return S[x];
}
int main() {
    
    
	int t;cin >> t;
	while (t--) {
    
    
		S.clear();
		n = 0;
		
		scanf("%d", &m);
		for (int i = 0; i < m;++i) {
    
    
			int x, y, e;
			scanf("%d%d%d", &x, &y, &e);
			query[i] = {
    
     get(x),get(y),e };
		}
		
		for(int i = 1; i <= n;++i)f[i] = i;
		bool has = false;
		for (int i = 0; i < m;++i) {
    
    
			if (query[i].e == 1)f[Find(query[i].x)] = Find(query[i].y);
		}

		for (int i = 0;i < m;++i) {
    
    
			if (query[i].e == 0 && Find(query[i].x) == Find(query[i].y)) {
    
    
				has = true;
				break;
			}
		}

		if (has)puts("NO");
		else puts("YES");
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/zzq0523/article/details/113099578