fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll int
  6. #define pll pair < int, int >
  7. #define debug(x) 0
  8.  
  9. // 1 indexing
  10. struct LCA {
  11. ll LOG, n;
  12. vector < vector < pll >> adj;
  13. vector < vector < ll >> btree;
  14. vector < ll > depth;
  15.  
  16. LCA(ll _n, vector < vector < pll >> & _adj) // no of nodes
  17. {
  18. n = _n;
  19. LOG = log2(n) + 1;
  20. btree.resize(n + 1, vector < ll > (LOG));
  21. adj = _adj;
  22. depth.resize(n + 1, 0);
  23. }
  24.  
  25. void make_tree(ll a, ll prv = -1) {
  26. for (auto[b, w]: adj[a]) {
  27. if (b == prv) continue;
  28.  
  29. depth[b] = depth[a] + 1;
  30. btree[b][0] = a; // a is parent of b
  31. for (int j = 1; j < LOG; j++) btree[b][j] = btree[btree[b][j - 1]][j - 1];
  32. make_tree(b, a);
  33. }
  34. }
  35.  
  36. ll find_lca(ll u, ll v) {
  37. if (depth[u] < depth[v]) swap(u, v);
  38. int k = depth[u] - depth[v];
  39. for (int i = LOG - 1; i >= 0; i--) {
  40. if (k & (1 << i)) u = btree[u][i];
  41. }
  42. if (u == v) return u;
  43. for (int i = LOG - 1; i >= 0; i--) {
  44. if (btree[u][i] != btree[v][i]) {
  45. u = btree[u][i];
  46. v = btree[v][i];
  47. }
  48. }
  49. return btree[u][0];
  50. }
  51. };
  52.  
  53. void get_sum(int curr, int prv, vector < int > & ans, vector < vector < pll >> & adj, vector < vector < ll >> & psum) {
  54. psum[curr] = ans;
  55. for (auto[x, w]: adj[curr]) {
  56. if (x == prv) continue;
  57. ans[w]++;
  58. get_sum(x, curr, ans, adj, psum);
  59. ans[w]--;
  60. }
  61. }
  62.  
  63. vector < int > solve(int n, vector < vector < pll >> & adj, int q, vector < vector < int >> & queries) {
  64.  
  65. vector < vector < ll >> psum(n + 1, vector < ll > (27, 0));
  66. LCA lc(n, adj);
  67. lc.make_tree(1);
  68.  
  69. vector < ll > f_arr(27, 0);
  70. get_sum(1, 0, f_arr, adj, psum);
  71.  
  72. debug(psum[1]);
  73.  
  74. vector < int > res;
  75. res.reserve(q);
  76. for (int qq = 0; qq < q; qq++) {
  77. int a = queries[qq][0];
  78. int b = queries[qq][1];
  79.  
  80. int par = lc.find_lca(a, b), tot = 0, cnt = 0, val = 0;
  81. fill(f_arr.begin(), f_arr.end(), 0);
  82.  
  83. for (int i = 0; i < 27; i++) {
  84. f_arr[i] += (psum[a][i] + psum[b][i]) - 2 * psum[par][i];
  85. tot += f_arr[i];
  86. }
  87. cnt = (tot + 1) / 2;
  88. tot = 0;
  89. for (int i = 0; i < 27; i++) {
  90. tot += f_arr[i];
  91. if (tot >= cnt) {
  92. cnt = i;
  93. break;
  94. }
  95. }
  96. int ans = 0;
  97.  
  98. for (int i = 0; i < 27; i++) {
  99. ans += f_arr[i] * abs(i - cnt);
  100. }
  101. res.push_back(ans);
  102. }
  103. return res;
  104. }
  105.  
  106. int main() {
  107.  
  108. ios::sync_with_stdio(0);
  109. cin.tie(0);
  110. int n;
  111. cin >> n;
  112. vector < vector < int > > edges(n - 1, vector < int > (3));
  113. for (int i_edges = 0; i_edges < n - 1; i_edges++) {
  114. for (int j_edges = 0; j_edges < 3; j_edges++) {
  115. cin >> edges[i_edges][j_edges];
  116. }
  117. }
  118.  
  119. vector < vector < pll >> adj(n + 1);
  120. for (int i = 0; i < n - 1; i++) {
  121. int u, v, w;
  122. u = edges[i][0];
  123. v = edges[i][1];
  124. w = edges[i][2];
  125. adj[u].push_back({
  126. v,
  127. w
  128. });
  129. adj[v].push_back({
  130. u,
  131. w
  132. });
  133. }
  134.  
  135. int Q;
  136. cin >> Q;
  137. vector < vector < int > > Queries(Q, vector < int > (2));
  138. for (int i_Queries = 0; i_Queries < Q; i_Queries++) {
  139. for (int j_Queries = 0; j_Queries < 2; j_Queries++) {
  140. cin >> Queries[i_Queries][j_Queries];
  141. }
  142. }
  143.  
  144. vector < int > out_;
  145. out_ = solve(n, adj, Q, Queries);
  146. cout << out_[0];
  147. for (int i_out_ = 1; i_out_ < out_.size(); i_out_++) {
  148. cout << " " << out_[i_out_];
  149. }
  150. }
Success #stdin #stdout 0.01s 5360KB
stdin
3
1 2 3
1 3 2
1
2 3
stdout
1