fork(1) download
  1. using namespace std;
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #include<math.h>
  6. #include<string.h>
  7. int a[10001],ans[10001];
  8.  
  9. int find(int x,int ans[] )
  10. {
  11. if(ans[x]==x)
  12. return x;
  13. else
  14. {
  15. int result=find(ans[x],ans);
  16. ans[x]=result;
  17. return result;
  18. }
  19. }
  20.  
  21. void merge(int b,int c,int a[],int ans[])
  22. {
  23. if(a[b]>a[c])
  24. ans[c]=b;
  25. //else edit here
  26. else if(a[b]<a[c])
  27. ans[b]=c;
  28. }
  29. int main()
  30. {
  31. int t,n,i,j,k,l;
  32. cin>>t;
  33. int x,y,z,q;
  34. while(t--)
  35. {
  36. cin>>n;
  37. for(i=1;i<n+1;i++)
  38. {
  39. cin>>a[i];
  40. ans[i]=i;
  41. }
  42. cin>>q;
  43. while(q--)
  44. {
  45. cin>>x;
  46. if(x==0)
  47. {
  48. cin>>y>>z;
  49. y=find(y,ans);
  50. z=find(z,ans);
  51. if(y==z)
  52. {
  53. cout<<"Invalid query!"<<endl;
  54. continue;
  55. }
  56. else
  57. merge(y,z,a,ans);
  58. }
  59. else
  60. {
  61. cin>>y;
  62.  
  63. cout<<ans[y]<<endl;
  64.  
  65. }
  66. }
  67. }
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 3376KB
stdin
1
5
3 5 100000 5 4
9
0 2 3
0 1 5
1 2 
1 1
0 3 5
1 3
1 2
1 5
1 1
stdout
3
5
3
3
3
5