23pts 求调
查看原帖
23pts 求调
979363
zxr_zwgh楼主2024/7/26 11:22

rt,样例过了,全MLE

#include<bits/stdc++.h>
using namespace std;
int dist[100005],rc[100005],lc[100005],rt[100005];
bool tf[100005];
struct node{
	int id,v;
	bool operator<(node x){ return v==x.v?id<x.id:v<x.v;}
}v[100005];
int find(int x){return rt[x]==x?x:rt[x]=find(rt[x]);}
int merge(int x,int y){
	if(!x||!y) return x+y;
	if(v[y]<v[x]) swap(x,y);
	rc[x]=merge(rc[x],y);
	if(dist[lc[x]]<dist[rc[x]]) swap(lc[x],rc[x]);
	dist[x]=dist[rc[x]]+1;
	return x;
}
int main(){
	dist[0]=-1;
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>v[i].v;
		rt[i]=i;
		v[i].id=i;
	}
	while(m--){
		int op;
		cin>>op;
		if(op==1){
			int x,y;
			cin>>x>>y;
			if(tf[x]||tf[y]){
				continue;
			}
			x=find(x),y=find(y);
			if(x!=y) rt[x]=rt[y]=merge(x,y);
		}if(op==2){
			int x;
			cin>>x;
			if(tf[x]) cout<<-1<<"\n";
			x=find(x);
			cout<<v[x].v<<"\n";
			tf[x]=1;
			rt[lc[x]]=rt[rc[x]]=rt[x]=-1;
			rc[x]=lc[x]=dist[x]=0;
		}
	}
	return 0;
}
2024/7/26 11:22
加载中...