天天育儿网,内容丰富有趣,生活中的好帮手!
天天育儿网 > 【洛谷3377】 左偏树(可并堆)

【洛谷3377】 左偏树(可并堆)

时间:2024-02-16 16:11:22

相关推荐

【洛谷3377】 左偏树(可并堆)

前言

其实我是不小心翻线性基的时候看见的。

Solution

左偏树只会模板,挖坑待补

代码实现

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<algorithm>#include<queue>#include<set>#include<map>#include<iostream>using namespace std;#define ll long long#define re register#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)inline int gi(){int f=1,sum=0;char ch=getchar();while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}return f*sum;}const int N=100010;int f[N],dis[N],ch[N][2],v[N],n,m;int find(int x){while(f[x])x=f[x];return x;}int merge(int x,int y){if(!x || !y)return x+y;if(v[x]>v[y] || (v[x]==v[y] && x>y))swap(x,y);ch[x][1]=merge(ch[x][1],y);f[ch[x][1]]=x;if(dis[ch[x][0]]<dis[ch[x][1]])swap(ch[x][0],ch[x][1]);dis[x]=dis[ch[x][1]]+1;return x;}int main(){n=gi();m=gi();dis[0]=-1;for(int i=1;i<=n;i++)v[i]=gi();while(m--){int opt=gi();if(opt==1){int x=gi(),y=gi();if(v[x]==-1 || v[y]==-1)continue;if(find(x)==find(y))continue;merge(find(x),find(y));}else{int x=gi(),F=find(x);printf("%d\n",v[F]);if(v[F]==-1)continue;v[F]=-1;f[ch[F][0]]=f[ch[F][1]]=0;merge(ch[F][0],ch[F][1]);}}return 0;}

如果觉得《【洛谷3377】 左偏树(可并堆)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
HDU1512 (左偏树)

HDU1512 (左偏树)

2023-12-21

左偏树原理详解

左偏树原理详解

2023-06-29

【左偏树】讲解

【左偏树】讲解

2024-02-04

左偏树/左倾堆

左偏树/左倾堆

2023-06-27

【算法学习】左偏树

【算法学习】左偏树

2021-11-15