Advertisement
TheAnshul

XOR tree(incomplete) 1.0.1

Jun 27th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /***************************************************************************
  2. * ####### # *
  3. * # # # ###### # # # # #### # # # # # *
  4. * # # # # # # ## # # # # # # # *
  5. * # ###### ##### # # # # # #### ###### # # # *
  6. * # # # # ####### # # # # # # # # # *
  7. * # # # # # # # ## # # # # # # # *
  8. * # # # ###### # # # # #### # # #### ###### *
  9. ***************************************************************************/
  10. #include<bits/stdc++.h>
  11. #define ll long long
  12. #define pb push_back
  13. #define endl '\n'
  14. #define pii pair<ll int,ll int>
  15. #define vi vector<ll int>
  16. #define all(a) (a).begin(),(a).end()
  17. #define F first
  18. #define S second
  19. #define sz(x) (ll int)x.size()
  20. #define hell 1000000007
  21. #define rep(i,a,b) for(ll int i=a;i<b;i++)
  22. #define lbnd lower_bound
  23. #define ubnd upper_bound
  24. #define bs binary_search
  25. #define mp make_pair
  26. using namespace std;
  27.  
  28. #define N 100005
  29. vi ad[N];
  30. ll v[N];
  31. bool vis[N];
  32. ll xr[N];
  33. pii papa[N];
  34. ll srt;
  35. // ll tree[4*N];
  36. ll lazy[4*N];
  37. void dfs(ll node,ll par,ll pari)
  38. {
  39. papa[node].F=par;
  40. papa[node].S=pari+1;
  41. srt^=v[node];
  42. xr[node]=srt;
  43. vis[node]=1;
  44. for(auto i:ad[node])
  45. if(!vis[i])
  46. dfs(i,node,pari+1);
  47. srt^=v[node];
  48. return;
  49. }
  50. // void build(ll node,ll start,ll end)
  51. // {
  52. // if(start==end)
  53. // {
  54. // tree[node]=v[start];
  55. // }
  56. // ll mid=(start+end)/2;
  57. // build(2*node,start,mid);
  58. // build(2*node,mid+1,end);
  59. // }
  60. ll findingDaddy(ll node1,ll node2)
  61. {
  62. if(papa[node1].S>papa[node2].S)
  63. {
  64. while(papa[node1].S!=papa[node2].S)
  65. {
  66. node1=papa[node1].F;
  67. }
  68. }
  69. else
  70. {
  71. while(papa[node1].S!=papa[node2].S)
  72. {
  73. node2=papa[node2].F;
  74. }
  75. }
  76. while(node1!=node2)
  77. {
  78. node1=papa[node1].F;
  79. node2=papa[node2].F;
  80. }
  81. return(node1);
  82. }
  83. int main()
  84. {
  85. ios_base::sync_with_stdio(false);
  86. cin.tie(0);
  87. cout.tie(0);
  88. int TESTS=1;
  89. cin>>TESTS;
  90. while(TESTS--)
  91. {
  92. ll n,q,x,y;
  93. cin>>n;
  94. rep(i,1,n+1)
  95. cin>>v[i];
  96. rep(i,1,n)
  97. {
  98. cin>>x>>y;
  99. ad[x].pb(y);
  100. ad[y].pb(x);
  101. }
  102. dfs(1,-1,0);
  103. cin>>q;
  104. ll t;
  105. // rep(i,1,n+1)
  106. // cout<<xr[i]<<" ";
  107. // cout<<endl;
  108. // rep(i,1,n+1)
  109. // cout<<papa[i].F<<" ";
  110. // cout<<endl;
  111. // rep(i,1,n+1)
  112. // cout<<papa[i].S<<" ";
  113. // cout<<endl;
  114. rep(i,0,q)
  115. {
  116. cin>>t>>x>>y;
  117. // cout<<findingDaddy(x,y);
  118. if(t==1)
  119. {
  120. //need to work on update.
  121. }
  122. if(t==2)
  123. cout<<((xr[x]^xr[y])^(v[findingDaddy(x,y)]))<<endl;
  124. }
  125. rep(i,1,n+1)
  126. ad[i].clear();
  127. }
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement