/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|      8|def build_step_1(int u, int d, int p) -> void {
   23|      8|  size[u] = 1;
   24|      8|  depth[u] = d;
   25|      8|  parent[u] = p;
   26|     22|  for (int e = head[u]; e; e = edge[e].nxt) {
                                         ^14
  ------------------
  |  Branch (26:25): [True: 63.64%, False: 36.36%]
  ------------------
   27|     14|    if (int v = edge[e].to; v != p) {
  ------------------
  |  Branch (27:29): [True: 50.00%, False: 50.00%]
  ------------------
   28|      7|      build_step_1(v, d + 1, u);
   29|      7|      size[u] += size[v];
   30|      7|      if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
                                         ^3
  ------------------
  |  Branch (30:11): [True: 57.14%, False: 42.86%]
  |  Branch (30:28): [True: 66.67%, False: 33.33%]
  ------------------
   31|      6|        heavy[u] = v;
   32|      6|      }
   33|      7|    }
   34|     14|  }
   35|      8|}
   36|       |
   37|      8|def build_step_2(int u, int a, int p) -> void {
   38|      8|  node2id[u] = id;
   39|      8|  id2node[id] = u;
   40|      8|  ++id;
   41|      8|  ances[u] = a;
   42|      8|  if (heavy[u]) build_step_2(heavy[u], a, u);
                              ^4
  ------------------
  |  Branch (42:7): [True: 50.00%, False: 50.00%]
  ------------------
   43|     22|  for (int e = head[u]; e; e = edge[e].nxt) {
                                         ^14
  ------------------
  |  Branch (43:25): [True: 63.64%, False: 36.36%]
  ------------------
   44|     14|    if (int v = edge[e].to; v != heavy[u] && v != p) {
                                                           ^10
  ------------------
  |  Branch (44:29): [True: 71.43%, False: 28.57%]
  |  Branch (44:46): [True: 30.00%, False: 70.00%]
  ------------------
   45|      3|      build_step_2(v, v, u);
   46|      3|    }
   47|     14|  }
   48|      8|}
   49|       |
   50|     13|def lca(int u, int v) -> int {
   51|     13|  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|     13|    while (depth[u] > depth[v]) {
  ------------------
  |  Branch (52:12): [True: 0.00%, False: 100.00%]
  ------------------
   53|      0|      u = parent[u];
   54|      0|    }
   55|     18|    while (depth[v] > depth[u]) {
  ------------------
  |  Branch (55:12): [True: 27.78%, False: 72.22%]
  ------------------
   56|      5|      v = parent[v];
   57|      5|    }
   58|     30|    while (u != v) {
  ------------------
  |  Branch (58:12): [True: 56.67%, False: 43.33%]
  ------------------
   59|     17|      u = parent[u];
   60|     17|      v = parent[v];
   61|     17|    }
   62|     13|    return u;
   63|     13|  }
   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|     13|}
   73|       |
   74|     10|def jump(int u, int d) -> int {
   75|     10|  if (int t = depth[u] - d; t < 32) {
  ------------------
  |  Branch (75:29): [True: 100.00%, False: 0.00%]
  ------------------
   76|     16|    while (t--) u = parent[u];
                              ^6
  ------------------
  |  Branch (76:12): [True: 37.50%, False: 62.50%]
  ------------------
   77|     10|    return u;
   78|     10|  }
   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|     10|}
   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|      8|  for (int i = 1; i < n; ++i) {
                                       ^7
  ------------------
  |  Branch (98:19): [True: 87.50%, False: 12.50%]
  ------------------
   99|      7|    int a = rd.uh();
  100|      7|    int b = rd.uh();
  101|      7|    edge[i * 2 | 0] = {b, head[a]}, head[a] = i * 2 | 0;
  102|      7|    edge[i * 2 | 1] = {a, head[b]}, head[b] = i * 2 | 1;
  103|      7|  }
  104|      1|  build_step_1(0, 0, -1);
  105|      1|  build_step_2(0, 0, -1);
  106|     14|  while (q--) {
  ------------------
  |  Branch (106:10): [True: 92.86%, False: 7.14%]
  ------------------
  107|     13|    int u = rd.uh();
  108|     13|    int v = rd.uh();
  109|     13|    int k = rd.uh();
  110|     13|    int w = lca(u, v);
  111|     13|    int du = depth[u];
  112|     13|    int dv = depth[v];
  113|     13|    int dw = depth[w];
  114|     13|    if (k <= du - dw) {
  ------------------
  |  Branch (114:9): [True: 46.15%, False: 53.85%]
  ------------------
  115|      6|      int x = jump(u, du - k);
  116|      6|      wt.uw(x);
  117|      7|    } else if (k <= du + dv - dw - dw) {
  ------------------
  |  Branch (117:16): [True: 57.14%, False: 42.86%]
  ------------------
  118|      4|      int x = jump(v, dw + dw - du + k);
  119|      4|      wt.uw(x);
  120|      4|    } else {
  121|      3|      wt.puts(" -1", 3);
  122|      3|    }
  123|     13|  }
  124|      1|  return 0;
  125|      1|}