21分求调
查看原帖
21分求调
754856
_zexal_楼主2023/1/12 20:52

rt,也不知道怎么回事

#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=(b);i++)
using namespace std;
const int Maxn=1e6;
int opt,a,b,c,n,m,x,y,k,tot;
bool book[Maxn];
struct node{
	int f,l,r,v,dist;
}llt[Maxn];
inline long long read() {
	long long x = 0, f = 1;
	char ch = getchar();
	while(ch<'0'||ch>'9') {
		if(ch == '-') f=-1;
		ch = getchar();
	}
	while(ch>='0'&&ch<='9') {
		x = ( x << 1 ) + ( x << 3 ) + ( ch ^ 48 );
		ch = getchar();
	}
	return x * f;
}
inline int find(int x){
	if(llt[x].f==x) return x;
	else return llt[x].f=find(llt[x].f);
}
inline int Merge(int x,int y){
	if(!x||!y) return x+y;
	if(llt[x].v>llt[y].v) swap(x,y);
	llt[x].r=Merge(llt[x].r,y);
	if(llt[llt[x].l].dist<llt[llt[x].r].dist) swap(llt[llt[x].l].dist,llt[llt[x].r].dist);
	llt[x].dist=llt[llt[x].r].dist+1;
	return x;
}
int main() {
	llt[0].dist=0;
	n=read(),m=read();
	F(i,1,n){
		llt[i].v=read();
		llt[i].f=i;
	} 
	F(i,1,m){
		cin>>opt;
		if(opt==1){
			a=read();
			b=read();
			x=find(a);
			y=find(b);
			if(book[a]||book[b]) continue;
			if(x!=y) llt[x].f=llt[y].f=Merge(x,y);
		}else{
			a=read();
			if(book[a]){
				cout<<-1<<endl;
				continue;
			}
			a=find(a);
			cout<<llt[a].v<<endl;
			book[a]=true;
			llt[llt[x].l].f=llt[llt[x].r].f=llt[x].f=Merge(llt[x].l,llt[x].r);
			llt[x].l=llt[x].r=llt[x].dist=0;
			
		}
	}
	return 0;
}


2023/1/12 20:52
加载中...