How can I calculate this with efficiency without using HLD or Link Cut Trees? I tried doing it with Binary Lifting but I am not able to do it correctly. Please guide me. Thanks.
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 155 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 145 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 134 |
| 9 | maroonrk | 132 |
| 10 | cry | 130 |
How can I calculate this with efficiency without using HLD or Link Cut Trees? I tried doing it with Binary Lifting but I am not able to do it correctly. Please guide me. Thanks.
| Name |
|---|



You are in the right path if you tried doing it using binary lifting. Have you managed to succeed in doing a regular binary lifting (just to find the LCA of 2 vertices)? If not I recommend you reading this blog
Now, if you understand how to find LCA of 2 vertices, you do almost the same thing you did for calculating the parenting array but instead you do it for the length of edges
// g is the adjacency list
// w is the "adjacency list" of weights
void DFS(int node, int cost, int par = -1) {
p[node][0] = par; maxi[node][0] = cost; if (par != -1) { h[node] = 1 + h[par]; } for (int i = 1; i < MAX_LOG; i++) { if (p[node][i - 1] != -1) { p[node][i] = p[p[node][i - 1]][i - 1]; maxi[node][i] = max(maxi[node][i - 1], maxi[p[node][i - 1]][i - 1]); } } for (int i = 0; i < (int)g[node].size(); i++) { int viz = g[node][i]; if (viz != par) { DFS(viz, w[node][i], node); } }}
For a query it is again very similar to finding the LCA of 2 vertices.