启发式并堆WA 30pts 求调玄关
查看原帖
启发式并堆WA 30pts 求调玄关
611878
sunqihuan楼主2025/1/23 16:06
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct node{
	int val,num;
	bool operator < (const node t) const{
		if(val!=t.val)return val>t.val;
		return num>t.num;
	}
}a[N];
priority_queue<node> q[N];
int n,T,fa[N],del[N];
int find(int x){
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
void merge(int x,int y){
	if(del[x] || del[y])return;
	x=find(x),y=find(y);
	if(x==y)return;
	if(q[x].size()>q[y].size())swap(x,y);
	fa[x]=y;
	while(q[x].size()){
		q[y].push(q[x].top());
		q[x].pop();
	}
}
int delmin(int x){
	x=find(x);
	node t=q[x].top();
	q[x].pop();
	del[t.num]=1;
	return t.val;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>T;
	for(int i=1;i<=n;i++){
		cin>>a[i].val;
		fa[i]=i;
		a[i].num=i;
		q[i].push(a[i]);
	}
	while(T--){
		int op,x,y;
		cin>>op;
		if(op==1){
			cin>>x>>y;
			merge(x,y);
		}else{
			cin>>x;
			if(del[x])puts("-1");
			else cout<<delmin(x)<<"\n";
		}
	}
	return 0;
}


2025/1/23 16:06
加载中...