P1955 [NOI2015] program automatically analyze discrete

Original link: http://www.cnblogs.com/bxd123/p/11003237.html

  

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: Suppose variables x1, x2, x3 ... represent program appearing, given n of the form xi = xj xi ≠ xj variables or equal / unequal constraints, please determination It can respectively assigned to an appropriate value for each variable, such that all of the above constraints are simultaneously satisfied. For example, a constraint problem is: x1 = x2, x2 = x3, x3 = x4, x4 ≠ x1, these constraints can not obviously be met simultaneously, so this problem can not be determined to be satisfied.

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

Input and output formats

Input formats:

 

Read data from files prog.in in.

The input file comprises a first line a positive integer t, it indicates the number of issues to be determined. Note that these are separate issues between.

For each issue contains several lines:

Line 1 contains a positive integer n, the number of constraints that need to issue conditions are satisfied. Subsequently n rows, each row comprising three integers i, j, e, described an equal / unequal constraints, separated by a single space between adjacent integers. If e = 1, the constraint is xj xi =; if e = 0, then the constraint is xi ≠ xj;

 

Output formats:

 

Prog.out the output to a file.

Output file including t rows.

K-th row of the output file output a character string "YES" or "NO" (without the quotes, all capital letters), "YES" denotes the k-th input is determined that the problem can be satisfied, "NO" indicates not be Satisfy.

 

Sample input and output

Input Sample # 1:  Copy
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
Output Sample # 1:  Copy
NO
YES
Input Sample # 2:  Copy
2
3
1 2 1
2 3 1
3 1 1
4
1 2 1
2 3 1
3 4 1
1 4 0
Output Sample # 2:  Copy
YES
NO

Explanation

[1] The sample explanation

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

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

Sample Description [2]

In the first problem, there are three constraints: x1 = x2, x2 = x3, x3 = x1. Just so that the assignment x1 = x1 = x1, can simultaneously satisfy all of the constraints.

In the second problem, there are four constraints: x1 = x2, x2 = x3, x3 = x4, x4 ≠ x1. By the first three constraints can be introduced x1 = x2 = x3 = x4, but finally it required a constraint x1 ≠ x4, and therefore can not be satisfied.

【data range】

[Time 2s, memory 512M]

 

Problem is very simple disjoint-set  

But because too much data needs discretization

#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=2e6+5;
int f[N];
int find1(int x)
{
    return f[x]==x?x:f[x]=find1(f[x]);
}
void union1(int a,int b)
{
    int x=find1(a),y=find1(b);
    if(x!=y)
        f[x]=y;
}
int a[N],book[N],n,m,cnt;
struct node
{
    int a,b,c;
}s[N];
bool cmp(node a,node b)
{
    return a.c>b.c;
}
int main()
{
    int cas;RI(cas);
    while(cas--)
    {
        RI(n);
        rep(i,1,n)
        {
            RIII(s[i].a,s[i].b,s[i].c);
            book[++cnt]=s[i].a,book[++cnt]=s[i].b;
        }
        sort(book+1,book+1+cnt);
        int e=unique(book+1,book+1+cnt)-book;
        rep(i,1,e)f[i]=i;
        sort(s+1,s+1+n,cmp);
        int ok=1;
        rep(i,1,n)
        {
            int x=find1( lower_bound(book+1,book+e,s[i].a)-book );
            int y=find1( lower_bound(book+1,book+e,s[i].b)-book );
            if(s[i].c)
            f[x]=y;
            else if(x==y)
            {
                printf("NO\n");
                ok=0;
                break;
            }
        }
        if(ok)printf("YES\n");
    }
    return 0;
}
View Code

 

Reproduced in: https: //www.cnblogs.com/bxd123/p/11003237.html

Guess you like

Origin blog.csdn.net/weixin_30938149/article/details/95028208