Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define vi vector<int>
  3. using namespace std;
  4. const int ILE = 100005;
  5. const int pot = (1<<19);
  6. int jump[21][ILE];
  7. int nopath[ILE];
  8. int deep[ILE];
  9. int dr[pot*2];
  10. vi paths[ILE];
  11. int path[ILE];
  12. int start[ILE];
  13. int card[ILE];
  14. int id[ILE];
  15. vi g[ILE];
  16. int p_num = 1;
  17. int id_num = 0;
  18.  
  19. bool light(int a, int b)
  20. {
  21. return card[b]*2 >= card[a];
  22. }
  23.  
  24. void dfs1(int x, int pop)
  25. {
  26. deep[x] = deep[pop]+1;
  27. jump[0][x] = pop;
  28. card[x] = 1;
  29. for(auto it : g[x])
  30. {
  31. if(it != pop)
  32. {
  33. dfs1(it, x);
  34. card[x] += card[it];
  35. }
  36. }
  37. }
  38.  
  39. void dfs2(int x, int pop)
  40. {
  41. for(auto it : g[x])
  42. {
  43. if(it != pop)
  44. {
  45. if(light(x, it))
  46. {
  47. if(path[x])
  48. {
  49. path[it] = path[x];
  50. paths[path[x]].push_back(it);
  51. }
  52. else
  53. {
  54. path[it] = p_num++;
  55. paths[path[it]].push_back(it);
  56. start[path[it]] = it;
  57. }
  58. }
  59. dfs2(it, x);
  60. }
  61. }
  62. }
  63.  
  64. int lca(int a, int b)
  65. {
  66. if(deep[a] < deep[b])swap(a, b);
  67. for(int i=20; i>=0; i--)
  68. {
  69. if(deep[jump[i][a]] >= deep[b])a = jump[i][a];
  70. }
  71. if(a == b)return b;
  72. for(int i=20; i>=0; i--)
  73. {
  74. if(jump[i][a] != jump[i][b])
  75. {
  76. a = jump[i][a];
  77. b = jump[i][b];
  78. }
  79. }
  80. return jump[0][a];
  81. }
  82.  
  83. void add(int a, int b)
  84. {
  85. if(deep[a] < deep[b])cout<<"WRONG";
  86. for(a = id[a] + pot, b += id[b] + pot+1; a<b; a/=2, b/=2)
  87. {
  88. if(a&1)dr[a++]++;
  89. if(b&1)dr[--b]++;
  90. }
  91. }
  92.  
  93. void odd(int a, int b)
  94. {
  95. if(deep[a] < deep[b])cout<<"WRONG";
  96. for(a = id[a] + pot, b = id[b] + pot+1; a<b; a/=2, b/=2)
  97. {
  98. if(a&1)dr[a++]--;
  99. if(b&1)dr[--b]--;
  100. }
  101. }
  102.  
  103. int ask(int a, int b, int ans = 0)
  104. {
  105. if(deep[a] < deep[b])
  106. swap(a, b);
  107. if(!path[a])
  108. return nopath[a];
  109. else
  110. {
  111. a = id[a] + pot;
  112. while(a > 0)
  113. ans += dr[a], a/=2;
  114. return ans;
  115. }
  116. }
  117.  
  118. void hld(int a, int b)
  119. {
  120. int LCA = lca(a, b);
  121. while(a != LCA)
  122. {
  123. if(!path[a])
  124. {
  125. nopath[a]++;
  126. a = jump[0][a];
  127. }
  128. else
  129. {
  130. add(start[path[a]], a);
  131. if(path[a] != path[LCA])
  132. {
  133. a = jump[0][start[path[a]]];
  134. }
  135. else
  136. {
  137. odd(start[path[a]], LCA);
  138. a = LCA;
  139. }
  140. }
  141. }
  142. while(b != LCA)
  143. {
  144. if(!path[b])
  145. {
  146. nopath[b]++;
  147. b = jump[0][b];
  148. }
  149. else
  150. {
  151. add(start[path[b]], b);
  152. if(path[b] != path[LCA])
  153. {
  154. b = jump[0][start[path[b]]];
  155. }
  156. else
  157. {
  158. odd(start[path[b]], LCA);
  159. b = LCA;
  160. }
  161. }
  162. }
  163. }
  164.  
  165. int main()
  166. {
  167. ios_base::sync_with_stdio(0);
  168. cin.tie(0);
  169. cout.tie(0);
  170. int n, m, a, b, q;
  171. cin>>n>>q;
  172. for(int i=0; i<n-1; i++)
  173. {
  174. cin>>a>>b;
  175. g[a].push_back(b);
  176. g[b].push_back(a);
  177. }
  178. dfs1(1, 0);
  179. for(int i=1; i<=20; i++)
  180. {
  181. for(int j=1; j<=n; j++)
  182. {
  183. jump[i][j] = jump[i-1][jump[i-1][j]];
  184. }
  185. }
  186. dfs2(1, 0);
  187. for(int i=1; i<p_num; i++)
  188. for(auto it : paths[i])
  189. {
  190. id[it] = id_num++;
  191. }
  192. char v;
  193. for(int i=0; i<q; i++)
  194. {
  195. cin>>v>>a>>b;
  196. if(v == 'P')
  197. hld(a, b);
  198. else
  199. cout<<ask(a, b)<<"\n";
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement