/tmp/solutions/build/jump_on_tree-slow.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|     29|  while (ances[u] != ances[v]) {
  ------------------
  |  Branch (51:10): [True: 55.17%, False: 44.83%]
  ------------------
   52|     16|    if (depth[ances[u]] > depth[ances[v]]) {
  ------------------
  |  Branch (52:9): [True: 31.25%, False: 68.75%]
  ------------------
   53|      5|      u = parent[ances[u]];
   54|     11|    } else {
   55|     11|      v = parent[ances[v]];
   56|     11|    }
   57|     16|  }
   58|     13|  return depth[u] < depth[v] ? u : v;
                                             ^5  ^8
  ------------------
  |  Branch (58:10): [True: 38.46%, False: 61.54%]
  ------------------
   59|     13|}
   60|       |
   61|     10|def jump(int u, int d) -> int {
   62|     12|  while (depth[ances[u]] > d) {
  ------------------
  |  Branch (62:10): [True: 16.67%, False: 83.33%]
  ------------------
   63|      2|    u = parent[ances[u]];
   64|      2|  }
   65|     10|  return id2node[node2id[u] - depth[u] + d];
   66|     10|}
   67|       |
   68|       |} // namespace
   69|       |
   70|      1|int main() {
   71|      1|  rd rd;
   72|      1|  wt wt;
   73|      1|  int n = rd.uh();
   74|      1|  int q = rd.uh();
   75|      1|#ifdef LOCAL
   76|      1|  id = 0;
   77|      1|  std::memset(head, 0, 4 * n);
   78|      1|  std::memset(heavy, 0, 4 * n);
   79|      1|  std::memset(parent, 0, 4 * n);
   80|      1|#endif
   81|      8|  for (int i = 1; i < n; ++i) {
                                       ^7
  ------------------
  |  Branch (81:19): [True: 87.50%, False: 12.50%]
  ------------------
   82|      7|    int a = rd.uh();
   83|      7|    int b = rd.uh();
   84|      7|    edge[i * 2 | 0] = {b, head[a]}, head[a] = i * 2 | 0;
   85|      7|    edge[i * 2 | 1] = {a, head[b]}, head[b] = i * 2 | 1;
   86|      7|  }
   87|      1|  build_step_1(0, 0, -1);
   88|      1|  build_step_2(0, 0, -1);
   89|     14|  while (q--) {
  ------------------
  |  Branch (89:10): [True: 92.86%, False: 7.14%]
  ------------------
   90|     13|    int u = rd.uh();
   91|     13|    int v = rd.uh();
   92|     13|    int k = rd.uh();
   93|     13|    int w = lca(u, v);
   94|     13|    int du = depth[u];
   95|     13|    int dv = depth[v];
   96|     13|    int dw = depth[w];
   97|     13|    if (k <= du - dw) {
  ------------------
  |  Branch (97:9): [True: 46.15%, False: 53.85%]
  ------------------
   98|      6|      int x = jump(u, du - k);
   99|      6|      wt.uw(x);
  100|      7|    } else if (k <= du + dv - dw - dw) {
  ------------------
  |  Branch (100:16): [True: 57.14%, False: 42.86%]
  ------------------
  101|      4|      int x = jump(v, dw + dw - du + k);
  102|      4|      wt.uw(x);
  103|      4|    } else {
  104|      3|      wt.puts(" -1", 3);
  105|      3|    }
  106|     13|  }
  107|      1|  return 0;
  108|      1|}