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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <iostream> #include <vector> #include <set> #include <iterator> using namespace std; const int maxn=1e5+10; int n,q,u,v,k,m,fa[maxn],p[maxn]; char op; struct cmp{ bool operator()(int a,int b){ if(a!=b) return p[a]<p[b]; } }; set <int,cmp > g[maxn]; int find(int x){ if(fa[x]!=x){ fa[x]=find(fa[x]); return fa[x]; }else{ return x; } } void unionn(int i,int j){ int i_fa=find(i); int j_fa=find(j); fa[j_fa]=i_fa; g[i_fa].insert(g[j_fa].begin(),g[j_fa].end()); g[j_fa].clear(); } inline void query1(){ unionn(u,v); } inline void query2(){ int v_fa=find(v),cnt=0; set<int>::iterator it=g[v_fa].begin(); if(g[v_fa].size()<k){ cout<<-1<<endl;return; } advance(it,k-1); cout<<*it<<endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>p[i]; } for(int i=1;i<=n;i++) fa[i]=i,g[i].insert(i); for(int _=1;_<=m;_++){ cin>>u>>v;query1(); } cin>>q; for(int _=1;_<=q;_++){ cin>>op; if(op=='B') cin>>u>>v,query1(); else cin>>v>>k,query2(); } cin>>n; }
|