求助
查看原帖
求助
239895
Yusani_huh楼主2021/3/20 23:16

这个代码改的我整个人都不好了...

查不出跟正解有什么区别

#include<bits/stdc++.h>
using namespace std;
int n,m,h[100003];
bool f[100003];
struct node{
	int ls,rs,d,nm;
}t[100003];
int fd(int a){  //并查集路径压缩
	if(h[a]!=a) h[a]=fd(h[a]);
	return h[a];
}
int merge(int x,int y){  //合并操作
	if(!x||!y) return x+y;
	if(t[x].nm>t[y].nm||t[x].nm==t[y].nm&&x>y)
		swap(x,y);
	t[x].rs=merge(t[x].rs,y);
	if(t[t[x].ls].d<t[t[x].rs].d)
		swap(t[x].ls,t[x].rs);
	t[x].d=t[t[x].rs].d+1;
	return x;
}
int main(){
	scanf("%d%d",&n,&m);
	t[0].d=-1;
	for(int i=1;i<=n;++i)
		scanf("%d",&t[i].nm),h[i]=i;
	int w=0,x=0,y=0;
	for(int i=1;i<=m;++i){
		scanf("%d",&w);
		if(w==1){  //操作一,合并
			scanf("%d%d",&x,&y);
			if(f[x]||f[y]) continue;
			int a=fd(x),b=fd(y);
			if(a!=b) h[a]=h[b]=merge(a,b);
		}else{  //操作二,删除
			scanf("%d",&x);
			if(f[x]){
				printf("-1\n");
				continue;
			}
			x=fd(x),f[x]=true;
			printf("%d\n",t[x].nm);
			h[t[x].ls]=h[t[x].rs]=h[x]=merge(t[x].ls,t[x].rs);
		}
	}
	return 0;
}

想喷尽管喷,但望各位不吝赐教 /kk

2021/3/20 23:16
加载中...