/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]]) {
                                         ^187k
  ------------------
  |  Branch (30:11): [True: 62.55%, False: 37.45%]
  |  Branch (30:28): [True: 40.35%, False: 59.65%]
  ------------------
   31|   388k|        heavy[u] = v;
   32|   388k|      }
   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);
                              ^312k
  ------------------
  |  Branch (42:7): [True: 62.55%, False: 37.45%]
  ------------------
   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) {
                                                           ^687k
  ------------------
  |  Branch (44:29): [True: 68.72%, False: 31.28%]
  |  Branch (44:46): [True: 27.24%, False: 72.76%]
  ------------------
   45|   187k|      build_step_2(v, v, u);
   46|   187k|    }
   47|   999k|  }
   48|   500k|}
   49|       |
   50|   500k|def lca(int u, int v) -> int {
   51|   500k|  if (depth[u] < 32 && depth[v] < 32) {
                                     ^113
  ------------------
  |  Branch (51:7): [True: 0.02%, False: 99.98%]
  |  Branch (51:24): [True: 0.00%, False: 100.00%]
  ------------------
   52|      0|    while (depth[u] > depth[v]) {
  ------------------
  |  Branch (52:12): [True: 0.00%, False: 0.00%]
  ------------------
   53|      0|      u = parent[u];
   54|      0|    }
   55|      0|    while (depth[v] > depth[u]) {
  ------------------
  |  Branch (55:12): [True: 0.00%, False: 0.00%]
  ------------------
   56|      0|      v = parent[v];
   57|      0|    }
   58|      0|    while (u != v) {
  ------------------
  |  Branch (58:12): [True: 0.00%, False: 0.00%]
  ------------------
   59|      0|      u = parent[u];
   60|      0|      v = parent[v];
   61|      0|    }
   62|      0|    return u;
   63|      0|  }
   64|  1.27M|  while (ances[u] != ances[v]) {
                ^500k
  ------------------
  |  Branch (64:10): [True: 60.78%, False: 39.22%]
  ------------------
   65|   774k|    if (depth[ances[u]] > depth[ances[v]]) {
  ------------------
  |  Branch (65:9): [True: 50.06%, False: 49.94%]
  ------------------
   66|   387k|      u = parent[ances[u]];
   67|   387k|    } else {
   68|   387k|      v = parent[ances[v]];
   69|   387k|    }
   70|   774k|  }
   71|   500k|  return depth[u] < depth[v] ? u : v;
                                             ^250k^249k
  ------------------
  |  Branch (71:10): [True: 50.02%, False: 49.98%]
  ------------------
   72|   500k|}
   73|       |
   74|   458k|def jump(int u, int d) -> int {
   75|   458k|  if (int t = depth[u] - d; t < 32) {
  ------------------
  |  Branch (75:29): [True: 0.25%, False: 99.75%]
  ------------------
   76|  17.1k|    while (t--) u = parent[u];
                              ^15.9k
  ------------------
  |  Branch (76:12): [True: 93.32%, False: 6.68%]
  ------------------
   77|  1.14k|    return u;
   78|  1.14k|  }
   79|   699k|  while (depth[ances[u]] > d) {
                ^457k
  ------------------
  |  Branch (79:10): [True: 34.63%, False: 65.37%]
  ------------------
   80|   242k|    u = parent[ances[u]];
   81|   242k|  }
   82|   457k|  return id2node[node2id[u] - depth[u] + d];
   83|   458k|}
   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: 45.79%, False: 54.21%]
  ------------------
  115|   228k|      int x = jump(u, du - k);
  116|   228k|      wt.uw(x);
  117|   271k|    } else if (k <= du + dv - dw - dw) {
  ------------------
  |  Branch (117:16): [True: 84.75%, False: 15.25%]
  ------------------
  118|   229k|      int x = jump(v, dw + dw - du + k);
  119|   229k|      wt.uw(x);
  120|   229k|    } else {
  121|  41.3k|      wt.puts(" -1", 3);
  122|  41.3k|    }
  123|   500k|  }
  124|      1|  return 0;
  125|      1|}