萌新求助
查看原帖
萌新求助
551803
BPG_ning楼主2022/2/17 21:29
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m;
int lson[maxn],rson[maxn],h[maxn],W[maxn],fa[maxn];
int rd(){
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)) if(ch=='-'){f=-1;ch=getchar();}
	while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void wr(int x){
	if(x<0){putchar('-');x=-x;}
	if(x>9)wr(x/10);
	putchar(x%10+'0');
	return ;
}
inline int find_set(int x){return fa[x]==x?x:fa[x]=find_set(fa[x]);}
inline int add(int x,int y){
	if(!x||!y) return x+y;
	
	if(W[x]>W[y]) swap(x,y);
	rson[x]=add(rson[x],y);
	fa[rson[x]]=x;
	
	h[x]=h[rson[x]]+1;
	
	if(h[rson[x]]>h[lson[x]])
		swap(lson[x],rson[x]);
	return x;
}
int main(){
	n=rd();	m=rd();
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=1;i<=n;i++)W[i]=rd();
	
	while(m--){
		int x,y;
		if(rd()==1){
			x=rd(); y=rd();
			if(W[x]==-1||W[y]==-1) continue;
			int fx=find_set(x),fy=find_set(y);
			if(fx==fy) continue;
			fa[x]=fa[y]=add(fx,fy);
		}else{
			x=rd();
			if(W[x]==-1){puts("-1");continue;}
			int fx=find_set(x);
			wr(W[fx]);
			puts("");
			W[fx]=-1;
			fa[fx]=fa[lson[fx]]=fa[rson[fx]]=add(lson[fx],rson[fx]);
			rson[x]=lson[x]=h[x]=0;
		}
	}
	return 0;
}

T了…………

2022/2/17 21:29
加载中...