Luo Gu P1955 [NOI2015] Program automatic analysis [union search, discretization]

  topic portal

 


 

Topic description

In the process of implementing 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 x1, x2, x3... represent the variables that appear in the program. Given n variables of the form xi=xj or xi≠xj equal/unequal constraints, please decide Is it possible to assign appropriate values ​​to each variable separately so that all the above constraints are satisfied at the same time. For example, the constraints in a problem are: x1=x2, x2=x3, x3=x4, x4≠x1, these constraints are obviously impossible to be satisfied at the same time, so the problem should be judged as unsatisfiable.

 

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

 

Input and output format

Input format: 

Read data from the file prog.in.

 

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, include several lines:

 

Line 1 contains a positive integer n, which represents the number of constraints that need to be satisfied in the problem. Next n lines, each line includes 3 integers i, j, e, describing an equality/inequality constraint, and adjacent integers are separated by a single space. If e=1, the constraint condition is xi=xj; if e=0, then the constraint condition is xi≠xj;

  

Output format: 

Output to file prog.out.

 

The output file includes t lines.

 

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

  

Input and output example

 

Input example #1: 
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
Sample output #1: 
NO
YES

 

Input example #2: 
2
3
1 2 1
2 3 1
3 1 1
4
1 2 1
2 3 1
3 4 1
1 4 0
Sample output #2: 
YES
NO

 

illustrate

【Example Explanation 1】

In the first problem, the constraints are: x1=x2, x1≠x2. These two constraints contradict each other and therefore cannot be satisfied simultaneously.

In the second problem, the constraints are: x1=x2, x1=x2. These two constraints are equivalent and can be satisfied simultaneously.

 

【Example Description 2】

在第一个问题中,约束条件有三个:x1=x2,x2=x3,x3=x1。只需赋值使得x1=x1=x1,即可同时满足所有的约束条件。

在第二个问题中,约束条件有四个:x1=x2,x2=x3,x3=x4,x4≠x1。由前三个约束条件可以推出x1=x2=x3=x4,然而最后一个约束条件却要求x1≠x4,因此不可被满足。

 

【数据范围】

 

【时限2s,内存512M】

 


 

  

  分析:一眼可知是并查集,问题就在离散化,数据范围肯定不能直接上。但是蒟蒻并不擅长离散化,一开始还好感人地打了个hash,搞了半天结果屏幕上wa声一片……然后翻了翻大佬们的博客,发现。。。模个数就行了(O.o)。。好吧,是我太蒻了。

  Code:

  

 1 //It is made by HolseLee on 24th Apr 2018
 2 //Luogu.org P1955
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<iostream>
 8 #include<iomanip>
 9 #include<algorithm>
10 using namespace std;
11 const int mod=528419;
12 const int N=1e5+7;
13 int n,T,cnt,fa[mod+7],rank[mod+7];
14 struct Ques{int x,y;}a[N];
15 inline int read()
16 {
17   char ch=getchar();int num=0;bool flag=false;
18   while(ch<'0'||ch>'9'){if(ch=='-')flag=true;ch=getchar();}
19   while(ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}
20   return flag?-num:num;
21 }
22 inline void ready()
23 {
24   cnt=0;
25   for(int i=1;i<=mod;i++)fa[i]=i;
26   for(int i=1;i<=mod;i++)rank[i]=1;
27 }
28 inline int find(int x)
29 {return fa[x]==x?x:fa[x]=find(fa[x]);}
30 inline void merge(int x,int y)
31 {
32   if(rank[x]<rank[y]){
33     fa[x]=y;}
34   else{
35     fa[y]=x;
36     if(rank[x]==rank[y])rank[x]++;}
37 }
38 inline int work()
39 {
40   for(int i=1;i<=n;i++){
41     int x=read();int y=read();int e=read();
42     x%=mod;y%=mod;
43     if(e)merge(find(x),find(y));
44     else{a[++cnt].x=x;a[cnt].y=y;}
45   }
46   bool flag=true;
47   for(int i=1;i<=cnt;i++)
48     if(find(a[i].x)==find(a[i].y))
49       {flag=false;break;}
50   if(flag)puts("YES");
51   else puts("NO");
52 }
53 int main()
54 {
55   T=read();
56   while(T--){
57     n=read();
58     ready();work();}
59   return 0;
60 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837716&siteId=291194637