1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <bits/stdc++.h> #include <ext/pb_ds/priority_queue.hpp> #pragma GCC optimize(3) using namespace std; inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } template <typename _Tp> inline void read(_Tp &x){ int f=1;x=0;char ch=nc(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=nc();} x*=f; } inline void write(long long n){ if(n==0) return; write(n/10); putchar(n%10+'0'); } struct ${ int s,id; inline bool operator < (const $ &p)const{if (s!=p.s)return s>p.s;else return id>p.id;} }; inline $ g(int a,int b){$ p;p.s=a,p.id=b;return p;} __gnu_pbds::priority_queue<$> q[100005];
int n,m,f[100005],x; bool s[100005]; int find(int n){return f[n]==n?n:f[n]=find(f[n]);} int main(){ read(n),read(m); for (int i=1;i<=n;i++)read(x),q[i].push(g(x,i)),f[i]=i; while (m--){ read(x); if (x==1){ int a,b; read(a),read(b); int fa=find(a),fb=find(b); if (fa==fb||s[a]||s[b])continue; if (q[fa].size()>q[fb].size())f[fb]=fa,q[fa].join(q[fb]); else f[fa]=fb,q[fb].join(q[fa]); }else{ int a; read(a); if (s[a]){puts("-1");continue;} int fa=find(a); write((q[fa].top()).s),puts(""),s[(q[fa].top()).id]=1,q[fa].pop(); } } }
|