P1955 程序自动分析 题解
1.[ABC347C] Ideal Holidays题解2.《扶苏的问题》题解3.[ABC366C] Balls and Bag Query 题解4.[ABC366D] Cuboid Sum Query 题解5.P7706 文文的摄影布置 题解6.[ABC370C] Word Ladder 题解7.P11020 「LAOI-6」Radiation 题解8.P11019 「LAOI-6」[太阳]] 请使用最新版手机 QQ 体验新功能 题解9.[ABC371D] 1D Country 题解10.[ABC371D] 1D Country 线段树解法
11.P1955 程序自动分析 题解
12.[ABC376E] Max × Sum 题解13.[ABC379D] Home Garden 题解14.P11276 第一首歌 题解15.[ABC380C] Move Segment 题解一道并查集的裸题,并查集存储传递性,后判断。主题思路十分简单,重点在于离散化与离线的处理。
离散化,为什么会想到离散化呢,观察数据范围
离线处理,并查集维护相等关系,由于相等具有传递性
#include <bits/stdc++.h>
#define seq(q, w, e) for (int q = w; q <= e; q++)
#define ll long long
using namespace std;
const int maxn = 1e5+10,mod=1e9+7;
struct node{
int x,y;
int e;
}a[maxn<<1];
int fa[maxn<<1];
void in_it(int n){
seq(i,1,2*n){
fa[i]=i;
a[i]={0,0,0};
}
}
int find(int x){
if(fa[x]==x){
return x;
}
return fa[x]=find(fa[x]);
}
void merge(int x,int y){
int f1=find(x),f2=find(y);
if(f1==f2) return;
fa[f2]=f1;
}
int t,tot,n;
ll x,y;
map<ll,int>mp;
bool cmp(node x,node y){
return x.e>y.e;
}
bool cmp1=0;
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin>>t;
while(t--){
mp.clear();
cmp1=0;
tot=0;
cin>>n;
in_it(n);
seq(i,1,n){
cin>>x>>y>>a[i].e;
if(!mp[x]) mp[x]=++tot;
if(!mp[y]) mp[y]=++tot;
a[i].x=mp[x];
a[i].y=mp[y];
}
sort(a+1,a+1+n,cmp);
int num=1;
while(a[num].e){
merge(a[num].x,a[num].y);
num++;
}
seq(i,num,n){
if(find(a[i].x)==find(a[i].y)){
cmp1=1;
break;
}
}
if(cmp1){
cout<<"NO"<<"\n";
}
else{
cout<<"YES"<<"\n";
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】