Question 248. Discretization - Luogu P1955 [NOI2015] Automatic Analysis of Programs


Question 248. Discretization - Luogu P1955 [NOI2015] Automatic Analysis of Programs


1. The topic

insert image description here
insert image description here
insert image description here

2. Problem solving

It is easy to think of using the union check to deal with this problem, to do the union of the two numbers that are equal, and then to check the two numbers that are not equal after the input is completed, to see if the roots are the same, if they are the same, then the original It is equal, and now the two numbers say they are not equal, so it is NO. However, since the data range of i, j is 1e9, which is too huge, it is obvious that when the parent array represents the corresponding father of the subscript, there is no way to directly represent the number with the subscript, and the range of n is 1e5 (it also reads 10 6 ), so the parent actually only needs to open 2e5+2 (2e6+2), so in fact, we only need to put i and j in an array for discretization, and use the discretized subscript to correspond to the parent The subscript in it, and then do and check the set operation. code show as below:

#include <bits/stdc++.h>

using namespace std;

const int maxn=2e6+2;

int arr[maxn],cnt;
vector<pair<int,int>> eql,noteql;
int parent[maxn];
int flag;

void init()
{
    
    
    flag=1;
    cnt=0;
    eql.clear(),noteql.clear();
    fill(parent,parent+maxn,-1);
}

int findRoot(int v0)
{
    
    
    if(parent[v0]<0)
    {
    
    
        return v0;
    }
    else
    {
    
    
        return parent[v0]=findRoot(parent[v0]);
    }
}

void unionSet(int v1,int v2)
{
    
    
    int root1=findRoot(v1);
    int root2=findRoot(v2);
    if(root1==root2)
    {
    
    
        return;
    }
    if(parent[root1]<parent[root2])
    {
    
    
        parent[root1]+=parent[root2];
        parent[root2]=root1;
    }
    else
    {
    
    
        parent[root2]+=parent[root1];
        parent[root1]=root2;
    }
    return;
}

int main()
{
    
    
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
    
    
        init();
        int n;
        cin>>n;
        for(int j=0;j<n;j++)
        {
    
    
            int a,b,c;
            cin>>a>>b>>c;
            if(c)
            {
    
    
                eql.push_back({
    
    a,b});
                arr[cnt++]=a;
                arr[cnt++]=b;
            }
            else
            {
    
    
                noteql.push_back({
    
    a,b});
                arr[cnt++]=a;
                arr[cnt++]=b;
            }
        }
        //数据离散化处理
        sort(arr,arr+cnt);
        int len=unique(arr,arr+cnt)-arr;
        for(int j=0;j<eql.size();j++)
        {
    
    
            int v1=upper_bound(arr,arr+len,eql[j].first)-arr;//找寻数对应的现数组下标
            int v2=upper_bound(arr,arr+len,eql[j].second)-arr;
            unionSet(v1,v2);
        }
        for(int j=0;j<noteql.size();j++)
        {
    
    
            int v1=upper_bound(arr,arr+len,noteql[j].first)-arr;
            int v2=upper_bound(arr,arr+len,noteql[j].second)-arr;
            if(findRoot(v1)==findRoot(v2))
            {
    
    
                flag=0;
                break;
            }
        }
        if(flag)
        {
    
    
            cout<<"YES"<<endl;
        }
        else
        {
    
    
            cout<<"NO"<<endl;
        }
    }
}

Three, discretized code template

//数据离散化处理
sort(arr,arr+cnt);
int len=unique(arr,arr+cnt)-arr;
upper_bound(arr,arr+len,x)-arr;//找寻x对应的现数组下标

Click me to see Niu Ren's detailed explanation of discretization!


Guess you like

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