Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long long int nodeweight[11234];
  6. int nodelevel[11234];
  7. int marc[11234];
  8. vector<pair<int, int> > grafo[11234]; // 1: node conectado, 2: dist;
  9. int ancestral[11234][14];
  10. int pai[11234];
  11. void readn(int n){
  12. for(int i = 0; i< n-1; i++){
  13. int a, b, c;
  14. scanf("%d %d %d", &a, &b, &c);
  15. //printf("li %d %d %d\n", a, b, c);
  16. grafo[a].push_back(make_pair(b,c));
  17. grafo[b].push_back(make_pair(a,c));
  18. }
  19. }
  20. void dfs(int x){
  21. marc[x] = 1;
  22. for(int i = 0; i< grafo[x].size(); i++){
  23.  
  24. int viz = grafo[x][i].first;
  25.  
  26. if(marc[viz] != 1){
  27. int preco = grafo[x][i].second;
  28. nodelevel[viz] = nodelevel[x] +1;
  29. nodeweight[viz] = nodeweight[x] + preco;
  30. pai[viz] = x;
  31. dfs(viz);
  32. }
  33.  
  34. }
  35.  
  36.  
  37.  
  38. }
  39. int LCA(int u , int v){
  40. if(nodelevel[u] < nodelevel[v]){
  41. swap(u,v) ;
  42. }
  43. //u is the node at a greater depth/lower level
  44. //So we have to raise u to be at the same
  45. //level as v.
  46. int dist = nodelevel[u] - nodelevel[v] ;
  47. //printf("dist %d\n",dist);
  48. while(dist > 0){
  49. int raise_by = log2(dist);
  50. u = ancestral[u][raise_by];
  51. dist -= (1<<raise_by) ;
  52.  
  53. }
  54. //printf("u %d\n",u);
  55. if(u == v){
  56. return u ;
  57. }
  58.  
  59. //Now we keep raising the two nodes by equal amount
  60. //Until each node has been raised uptill its (k-1)th ancestor
  61. //Such that the (k)th ancestor is the lca.
  62. //So to get the lca we just return the parent of (k-1)th ancestor
  63. //of each node
  64. //printf ("v %d\n", v);
  65. for(int j = 13 ; j >= 0 ; --j){
  66. //Checking P[u][j] != P[v][j] because if P[u][j] == P[v][j]
  67. //P[u][j] would be the Lth ancestor such that (L >= k)
  68. //where kth ancestor is the LCA
  69. //But we want the (k-1)th ancestor.
  70. if((ancestral[u][j] != -1) && (ancestral[u][j] != ancestral[v][j])){
  71. u = ancestral[u][j] ;
  72. v = ancestral[v][j] ;
  73. //printf("j %d, u %d v %d\n",j, u, v);
  74. }
  75. }
  76. return pai[u] ; //or parent[v]
  77. }
  78. int jumpk(int u, int k){
  79. int dist = k ;
  80. while(dist > 0){
  81. int raise_by = log2(dist);
  82. u = ancestral[u][raise_by];
  83. dist -= (1<<raise_by) ;
  84. }
  85. return u;
  86. }
  87.  
  88.  
  89. int main() {
  90. int t;
  91. //printf("%f\n", log2(10000));
  92. scanf("%d", &t);
  93. for(int testcase = 0; testcase< t; testcase++){
  94. if(testcase >0) printf("\n");
  95.  
  96. int n;
  97. scanf("%d", &n);
  98. for(int i = 0; i<= n; i++){
  99. grafo[i].clear();
  100. nodeweight[i] = -1;
  101. nodelevel[i] = -1;
  102. marc[i] = -1;
  103. pai[i] = -1;
  104. }
  105. readn(n);
  106.  
  107. nodelevel[1] = 0;
  108. nodeweight[1] = 0;
  109. dfs(1);
  110. for(int i = 1; i <= n; i++ ){
  111. //printf("nodelevel[%d] = %d, nodeweight = %d\n", i, nodelevel[i],nodeweight[i]);
  112.  
  113. }
  114. for(int i = 1 ; i <= n ; i++){
  115. for(int j = 0 ; j < 14 ; j++){
  116. ancestral[i][j] = -1;
  117. }
  118. }
  119.  
  120. //The first ancestor(2^0 th ancestor)
  121. //for every node is parent[node]
  122. for(int i = 1 ; i <= n ; i++){
  123. ancestral[i][0] = pai[i] ;
  124. // printf("pai[%d] = %d\n", i, pai[i]);
  125.  
  126. }
  127.  
  128. for(int j = 1; (1<<j) < n ; j++){
  129. for(int i = 1 ; i <= n ; i++){
  130. //If a node does not have a (2^(j-1))th ancestor
  131. //Then it does not have a (2^j)th ancestor
  132. //and hence P[i][j] should remain -1
  133. //Else the (2^j)th ancestor of node i
  134. //is the (2^(j-1))th ancestor of ((2^(j-1))th ancestor of node i)
  135. if(ancestral[i][j-1] != -1){
  136. ancestral[i][j] = ancestral[ancestral[i][j-1]][j-1] ;
  137. //printf("ancestral[%d][%d] = %d\n",i, j, ancestral[i][j] );
  138. }
  139. }
  140. }
  141. char coiso[5];
  142. scanf( " %s", coiso);
  143. while(coiso[1] != 'O'){
  144. int a, b;
  145. scanf("%d %d", &a, &b);
  146. int lca = LCA(a,b);
  147.  
  148. //printf("lca %d, %d = %d\n", a, b, lca);
  149. if(coiso[1] == 'I'){
  150. printf("%lld\n", nodeweight[a]+nodeweight[b]-2*nodeweight[lca]);
  151. }else if( coiso[1] == 'T'){
  152. int k;
  153. scanf("%d", &k);
  154. if(nodelevel[a] - nodelevel[lca] >= k){
  155. printf("%d\n", jumpk(a, k-1));
  156.  
  157. }else{
  158. k = nodelevel[a] - nodelevel[lca]+ nodelevel[b]-nodelevel[lca] -k +1;
  159. //printf("%d ", k);
  160. printf("%d\n", jumpk(b, k));
  161. }
  162.  
  163. }
  164.  
  165. scanf(" %s",coiso);
  166. }
  167.  
  168.  
  169.  
  170. }
  171.  
  172.  
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement