link
solve
模板题,没什么好说的,就是思考为什么要加
v<B.v||(v==B.v&&id<B.id)
有没有哪位大佬能指点指点,按照原理说直接拿(v)排就好了,(id)为什么会有影响的
code
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int N,M;
int lc[maxn],rc[maxn],dist[maxn],rt[maxn];
bool vis[maxn];
struct node{
int id,v;
bool operator <(node B)const {return v<B.v||(v==B.v&&id<B.id);}
}v[maxn];
int getfa(int x){return rt[x]==x?x:rt[x]=getfa(rt[x]);}
inline int read(){
int ret=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch<='9'&&ch>='0')ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
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(){
freopen("P3377.in","r",stdin);
freopen("P3377.out","w",stdout);
dist[0]=-1;
N=read();M=read();
for(int i=1;i<=N;i++)v[i].v=read(),v[i].id=i,rt[i]=i;
for(int i=1;i<=M;i++){
int op=read(),x=read();
if(op==1){
int y=read();
if(vis[x]||vis[y])continue;
x=getfa(x),y=getfa(y);
if(x!=y)rt[x]=rt[y]=merge(x,y);
}
else{
if(vis[x]){printf("-1
");continue;}
x=getfa(x);
printf("%d
",v[x].v);
vis[x]=1;
rt[lc[x]]=rt[rc[x]]=rt[x]=merge(lc[x],rc[x]);
}
}
return 0;
}