Solution to a problem [NOI2015] program automatically analyzes

It is said that the exam solution to a problem can write $ \ text {RP} $ ++?

This is still considered a title search and title catchment qwq I do it for a long time

--------------------------------------------------------

Title Description

During the implementation of the program of the automatic analysis, it is often necessary to determine whether the number of constraints can be satisfied simultaneously.

Consider a simplified version of a constraint satisfaction problem: assuming $ $ x_1, x_2 variable $ $, $ ... $ X_3 represent program appearing in a given $ \ text {n} $ shaped as a $ x_i = x_j $ or $ x_i ≠ x_j $ variable equal / unequal constraint criteria, determines whether or not each variable may be given appropriate value, so that all the above constraints are simultaneously satisfied. For example, a constraint problem is: $ x_1 = x_2 $, $ x_2 = x_3 $, $ x_3 = x_4 $, $ x_4 ≠ x_1 $, these constraints can not obviously be met simultaneously, so this problem should It is determined not to be satisfied.

Now give some constraint satisfaction problems, they were to be judged.

Input Format

Input file comprising a first row 1 $ $ $ $ 1 positive integers $ \ text {t} $, is the number of issues to be determined. Note that these are separate issues between.

For each issue contains several lines:

The first row contains $ $ $ 1 $ 1 positive integers $ \ text {n} $, indicates the number of constraint problem needs to be satisfied. Then $ \ text {n} $ rows, each row including integers $ 3 $ $ \ text {i, j, e} $, $ 1 $ Description equal / unequal constraints, a single space between adjacent integer separated. If $ \ text {e = 1} $, the constraint is $ x_i = x_j $; if $ \ text {e = 0} $, the constraint is $ x_i ≠ x_j $;

Output Format

Output file comprising $ \ text {t} $ line.

Of $ \ text {k} $ output line of the output file a string "$ \ text {YES} $" or "$ \ text {NO} $" (without the quotes, all capital letters), "$ \ text { YES} $ "represents $ \ text {k} $ question may be determined to satisfy the input," $ \ text {NO} $ "indicates not be satisfied.

------------------------------------------------------------

Determining whether the subject requires a plurality of equations and inequalities contradiction.

Since a plurality of equations has passed, it will occur with disjoint-set to maintain the relationship between variables.

If the current data is no solution, then clearly that there is a plurality of indirect satisfy equation $ \ text {x = y} $, but the title $ \ text {x ≠ y} $ condition occurs again.

Therefore, for each set $ \ text {x = y} $ constraints, the two elements combined, representatives of the two equal elements satisfy relations.

If the $ \ text {x ≠ y} $ constraints, checks whether the investigation focused on these two elements and, if so, outputs no solution; otherwise continues to judge, until the End Analyzing all conditions.

There are some details of the deal middle of the process need to pay attention to their problems :( also, of course, there are likely to be I did not think of any better way to deal with)

$ \ Text {. 1} in the data range subject $ $ \ text {i, j} $ large array certainly fit, can be discretized;

$ \ Text {2.} $ Our application, if there are two conditions and meet each other in a certain order (for example, a first row $ \ text {x = y} $, second row $ \ text {x ≠ y} $), then our program can also output normal; but if these two lines to exchange the input sequence, the program will output the wrong answer. At first I think is another disjoint-set to maintain the relationship between inequality, but later found that inequality is not transitive ... so we can consider the relationship between equal priority variables, and then again handle the relationship between variables are not equal, so We can ensure that the relationship will not be affected later when the query variable is not equal communication.

Summarize is: to give priority to dealing with the relationship between the variables equal, guaranteed not to affect the back of the relationship between the variables are not equal.

$ \ Text {3.} $ Because of $ \ text {n} $ of the relationship, every relationship there are $ \ text {2} $ variables, so the disjoint-set size of the array must be open twice!

The code ~

#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 1e6 + 5;

int t, n, tot, cnt;
int fa[N << 1], a[N << 1], temp[N << 1];

struct node {
	int x, y;
	bool z;
} ask[N];//存储查询 

inline int get (int x) {
	if (x == fa[x]) return x;
	return fa[x] = get(fa[x]);
}

inline void merge (int x, int y) {
	fa[get(x)] = get(y);
}

inline bool tmp (node g, node h) {
	return g.z > h.z;
}

inline void solve () {
	scanf("%d", &n);
	tot = cnt = 0;
	for (int i = 1; i <= n * 2; i++) fa[i] = i;//数组大小一定要开两倍啊 
	for (int I =. 1; I <= n-; I ++) { 
		scanf("%d%d%d", &ask[i].x, &ask[i].y, &ask[i].z);
		TEMP [TOT ++] = ASK [I] .x; 
		TEMP [TOT ++] = ASK [I] .y; 
	} 
	Sort (TEMP +. 1, TOT +. 1 + TEMP); 
	Sort (ASK +. 1, n-ASK + +. 1, tmp); // sort queries to ensure that the relationship between the priority variable equal to 
	a [++ CNT] TEMP = [. 1]; 
	for (int I = 2; I <= TOT; I ++) { 
		IF - a [++ CNT] TEMP = [I] (TEMP [I] TEMP = [I. 1]!); 
	} // discrete input variables 
	for (int I =. 1; I <= n-; I ++) { 
		int XX = lower_bound (A +. 1, A + CNT +. 1, ASK [I] .x) - A; 
		int YY = lower_bound (A +. 1, A + +. 1 CNT, ASK [I] .y) - A; 
		IF (ASK [I] .Z) Merge (XX, YY); 
		the else { 
			IF (GET (XX) == GET (YY)) { 
				the printf ( "NO \ the n-"); 
				return; 
			} // If the previous satisfy the equal relations but here they meet ranging relationship, no solution 
		} 
	} 
	the printf ( "YES \ n-") ; 
} 

int main () { 
	Scanf ( "% D", & T);
	while (t--) solve();
	return 0;
}

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/66ccffxym/p/11862887.html