93分求助,改不动了QAQ
查看原帖
93分求助,改不动了QAQ
455558
Imiya楼主2021/9/20 18:41
//配对堆 
#include<bits/stdc++.h>
#define nul NULL
using namespace std;
const int M=100010;
struct node{
	int val;
	bool del;
	node*bro,*son,*fa;
	node(){
		bro=son=nul;
		del=false;
        fa=this;
	}
}heap[M];
node*merge(node*x,node*y){
	if(x==nul)return y;
	if(y==nul)return x;
	if(x->val>y->val)swap(x,y);
	y->bro=x->son;
	x->son=y;
	return x;
}
node*merges(node*x){
	if(x==nul||x->bro==nul)return x;
	return merge(merge(x,x->bro),merges(x->bro->bro));
}
node*find(node*x){
	return x->fa==x?x:x->fa=find(x->fa);
}
int n,m;
void init(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>heap[i].val;
	}
}
int main(){
// 	freopen("C:\\in.txt","r",stdin);
	init();
	for(;m;m--){
		int mode;
		cin>>mode;
		if(mode-1){
			int id;
			cin>>id;
			if(heap[id].del){
				cout<<-1<<endl;
				continue;
			}
			node*root=find(&heap[id]);
			cout<<root->val<<endl;
			root->del=true;
			root->fa=merges(root->son);
			if(root->fa!=nul)root->fa->fa=root->fa;
		}
		else{
			int a,b;
			cin>>a>>b;
			if(heap[a].del||heap[b].del||heap[a].fa==heap[b].fa)continue;
			node*x=find(&heap[a]);
			node*y=find(&heap[b]);
			x->fa=y->fa=merge(x,y);
		}
	}
	return 0;
}

wa了第11个点qwq

2021/9/20 18:41
加载中...