/tmp/solutions/build/jump_on_tree-main.cpp:
    1|       |#include <common.h>
    2|       |prelude;
    3|       |
    4|       |namespace {
    5|       |
    6|       |constexpr int N = 5e5;
    7|       |
    8|       |struct {
    9|       |  int to;
   10|       |  int nxt;
   11|       |} edge[N * 2];
   12|       |int head[N];
   13|       |int size[N];
   14|       |int depth[N];
   15|       |int heavy[N];
   16|       |int ances[N];
   17|       |int parent[N];
   18|       |int node2id[N];
   19|       |int id2node[N];
   20|       |int id;
   21|       |
   22|   500k|def build_step_1(int u, int d, int p) -> void {
   23|   500k|  size[u] = 1;
   24|   500k|  depth[u] = d;
   25|   500k|  parent[u] = p;
   26|  1.49M|  for (int e = head[u]; e; e = edge[e].nxt) {
                                         ^999k
  ------------------
  |  Branch (26:25): [True: 66.67%, False: 33.33%]
  ------------------
   27|   999k|    if (int v = edge[e].to; v != p) {
  ------------------
  |  Branch (27:29): [True: 50.00%, False: 50.00%]
  ------------------
   28|   499k|      build_step_1(v, d + 1, u);
   29|   499k|      size[u] += size[v];
   30|   499k|      if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
                                         ^452k
  ------------------
  |  Branch (30:11): [True: 9.51%, False: 90.49%]
  |  Branch (30:28): [True: 0.03%, False: 99.97%]
  ------------------
   31|  47.6k|        heavy[u] = v;
   32|  47.6k|      }
   33|   499k|    }
   34|   999k|  }
   35|   500k|}
   36|       |
   37|   500k|def build_step_2(int u, int a, int p) -> void {
   38|   500k|  node2id[u] = id;
   39|   500k|  id2node[id] = u;
   40|   500k|  ++id;
   41|   500k|  ances[u] = a;
   42|   500k|  if (heavy[u]) build_step_2(heavy[u], a, u);
                              ^47.5k
  ------------------
  |  Branch (42:7): [True: 9.51%, False: 90.49%]
  ------------------
   43|  1.49M|  for (int e = head[u]; e; e = edge[e].nxt) {
                                         ^999k
  ------------------
  |  Branch (43:25): [True: 66.67%, False: 33.33%]
  ------------------
   44|   999k|    if (int v = edge[e].to; v != heavy[u] && v != p) {
                                                           ^547k
  ------------------
  |  Branch (44:29): [True: 54.75%, False: 45.25%]
  |  Branch (44:46): [True: 82.65%, False: 17.35%]
  ------------------
   45|   452k|      build_step_2(v, v, u);
   46|   452k|    }
   47|   999k|  }
   48|   500k|}
   49|       |
   50|   500k|def lca(int u, int v) -> int {
   51|   500k|  if (depth[u] < 32 && depth[v] < 32) {
  ------------------
  |  Branch (51:7): [True: 100.00%, False: 0.00%]
  |  Branch (51:24): [True: 100.00%, False: 0.00%]
  ------------------
   52|   547k|    while (depth[u] > depth[v]) {
  ------------------
  |  Branch (52:12): [True: 8.70%, False: 91.30%]
  ------------------
   53|  47.6k|      u = parent[u];
   54|  47.6k|    }
   55|   547k|    while (depth[v] > depth[u]) {
  ------------------
  |  Branch (55:12): [True: 8.69%, False: 91.31%]
  ------------------
   56|  47.5k|      v = parent[v];
   57|  47.5k|    }
   58|  1.00M|    while (u != v) {
  ------------------
  |  Branch (58:12): [True: 50.25%, False: 49.75%]
  ------------------
   59|   504k|      u = parent[u];
   60|   504k|      v = parent[v];
   61|   504k|    }
   62|   500k|    return u;
   63|   500k|  }
   64|      0|  while (ances[u] != ances[v]) {
  ------------------
  |  Branch (64:10): [True: 0.00%, False: 0.00%]
  ------------------
   65|      0|    if (depth[ances[u]] > depth[ances[v]]) {
  ------------------
  |  Branch (65:9): [True: 0.00%, False: 0.00%]
  ------------------
   66|      0|      u = parent[ances[u]];
   67|      0|    } else {
   68|      0|      v = parent[ances[v]];
   69|      0|    }
   70|      0|  }
   71|      0|  return depth[u] < depth[v] ? u : v;
  ------------------
  |  Branch (71:10): [True: 0.00%, False: 0.00%]
  ------------------
   72|   500k|}
   73|       |
   74|   450k|def jump(int u, int d) -> int {
   75|   450k|  if (int t = depth[u] - d; t < 32) {
  ------------------
  |  Branch (75:29): [True: 100.00%, False: 0.00%]
  ------------------
   76|   627k|    while (t--) u = parent[u];
                              ^177k
  ------------------
  |  Branch (76:12): [True: 28.28%, False: 71.72%]
  ------------------
   77|   450k|    return u;
   78|   450k|  }
   79|      0|  while (depth[ances[u]] > d) {
  ------------------
  |  Branch (79:10): [True: 0.00%, False: 0.00%]
  ------------------
   80|      0|    u = parent[ances[u]];
   81|      0|  }
   82|      0|  return id2node[node2id[u] - depth[u] + d];
   83|   450k|}
   84|       |
   85|       |} // namespace
   86|       |
   87|      1|int main() {
   88|      1|  rd rd;
   89|      1|  wt wt;
   90|      1|  int n = rd.uh();
   91|      1|  int q = rd.uh();
   92|      1|#ifdef LOCAL
   93|      1|  id = 0;
   94|      1|  std::memset(head, 0, 4 * n);
   95|      1|  std::memset(heavy, 0, 4 * n);
   96|      1|  std::memset(parent, 0, 4 * n);
   97|      1|#endif
   98|   500k|  for (int i = 1; i < n; ++i) {
                                       ^499k
  ------------------
  |  Branch (98:19): [True: 100.00%, False: 0.00%]
  ------------------
   99|   499k|    int a = rd.uh();
  100|   499k|    int b = rd.uh();
  101|   499k|    edge[i * 2 | 0] = {b, head[a]}, head[a] = i * 2 | 0;
  102|   499k|    edge[i * 2 | 1] = {a, head[b]}, head[b] = i * 2 | 1;
  103|   499k|  }
  104|      1|  build_step_1(0, 0, -1);
  105|      1|  build_step_2(0, 0, -1);
  106|   500k|  while (q--) {
  ------------------
  |  Branch (106:10): [True: 100.00%, False: 0.00%]
  ------------------
  107|   500k|    int u = rd.uh();
  108|   500k|    int v = rd.uh();
  109|   500k|    int k = rd.uh();
  110|   500k|    int w = lca(u, v);
  111|   500k|    int du = depth[u];
  112|   500k|    int dv = depth[v];
  113|   500k|    int dw = depth[w];
  114|   500k|    if (k <= du - dw) {
  ------------------
  |  Branch (114:9): [True: 59.20%, False: 40.80%]
  ------------------
  115|   296k|      int x = jump(u, du - k);
  116|   296k|      wt.uw(x);
  117|   296k|    } else if (k <= du + dv - dw - dw) {
                         ^203k^203k
  ------------------
  |  Branch (117:16): [True: 75.51%, False: 24.49%]
  ------------------
  118|   154k|      int x = jump(v, dw + dw - du + k);
  119|   154k|      wt.uw(x);
  120|   154k|    } else {
  121|  49.9k|      wt.puts(" -1", 3);
  122|  49.9k|    }
  123|   500k|  }
  124|      1|  return 0;
  125|      1|}