acwing-程序自动分析

本文探讨了程序自动分析中的关键技术——离散化和并查集。离散化在处理大量数据时能有效减少数据的范围和复杂性,而并查集作为一种数据结构,常用于维护集合的联通性问题。通过结合这两项技术,可以更高效地进行程序分析和优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

离散化+并查集

#include<bits/stdc++.h>
using namespace std;

#define x first
#define y second
typedef pair<int,int> PII;

const int N = 2e5;
int t;
int n;
vector<int> all;
vector<PII> ye;
vector<PII> no;
int f[N];

int find(int x)
{
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}

int findid(int x)
{
    return lower_bound(all.begin(),all.end(),x)-all.begin();
}

void check()
{
    for(auto it:ye)
    {
        int x=findid(it.x),y=findid(it.y);
        int fax=find(x),fay=find(y);
        if(fax!=fay) f[fax]=fay;
    }
    
    bool cannot=false;
    for(auto it:no){
        int x=findid(it.x),y=findid(it.y);
        int fax=find(x),fay=find(y);
        if(fax==fay){
            cannot=true;break;
        }
    }
    if(cannot) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
}

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        all.clear();
        ye.clear();
        no.clear();
        for(int i=0;i<N;i++) f[i]=i;
        while (n -- )
        {
            int x,y,op;cin>>x>>y>>op;
            all.push_back(x);all.push_back(y);
            if(op==1) ye.push_back({x,y});
            else no.push_back({x,y});
        }
        sort(all.begin(),all.end());
        all.erase(unique(all.begin(),all.end()),all.end());
        check();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值