/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|  6.61M|def build_step_1(int u, int d, int p) -> void {
   23|  6.61M|  size[u] = 1;
   24|  6.61M|  depth[u] = d;
   25|  6.61M|  parent[u] = p;
   26|  19.8M|  for (int e = head[u]; e; e = edge[e].nxt) {
                                         ^13.2M
  ------------------
  |  Branch (26:25): [True: 66.67%, False: 33.33%]
  ------------------
   27|  13.2M|    if (int v = edge[e].to; v != p) {
  ------------------
  |  Branch (27:29): [True: 50.00%, False: 50.00%]
  ------------------
   28|  6.61M|      build_step_1(v, d + 1, u);
   29|  6.61M|      size[u] += size[v];
   30|  6.61M|      if (heavy[u] == 0 || size[v] > size[heavy[u]]) {
                                         ^3.46M
  ------------------
  |  Branch (30:11): [True: 47.67%, False: 52.33%]
  |  Branch (30:28): [True: 19.68%, False: 80.32%]
  ------------------
   31|  3.83M|        heavy[u] = v;
   32|  3.83M|      }
   33|  6.61M|    }
   34|  13.2M|  }
   35|  6.61M|}
   36|       |
   37|  6.61M|def build_step_2(int u, int a, int p) -> void {
   38|  6.61M|  node2id[u] = id;
   39|  6.61M|  id2node[id] = u;
   40|  6.61M|  ++id;
   41|  6.61M|  ances[u] = a;
   42|  6.61M|  if (heavy[u]) build_step_2(heavy[u], a, u);
                              ^3.15M
  ------------------
  |  Branch (42:7): [True: 47.67%, False: 52.33%]
  ------------------
   43|  19.8M|  for (int e = head[u]; e; e = edge[e].nxt) {
                                         ^13.2M
  ------------------
  |  Branch (43:25): [True: 66.67%, False: 33.33%]
  ------------------
   44|  13.2M|    if (int v = edge[e].to; v != heavy[u] && v != p) {
                                                           ^8.76M
  ------------------
  |  Branch (44:29): [True: 66.26%, False: 33.74%]
  |  Branch (44:46): [True: 39.49%, False: 60.51%]
  ------------------
   45|  3.46M|      build_step_2(v, v, u);
   46|  3.46M|    }
   47|  13.2M|  }
   48|  6.61M|}
   49|       |
   50|  10.0M|def lca(int u, int v) -> int {
   51|  51.2M|  while (ances[u] != ances[v]) {
  ------------------
  |  Branch (51:10): [True: 80.48%, False: 19.52%]
  ------------------
   52|  41.2M|    if (depth[ances[u]] > depth[ances[v]]) {
  ------------------
  |  Branch (52:9): [True: 50.00%, False: 50.00%]
  ------------------
   53|  20.6M|      u = parent[ances[u]];
   54|  20.6M|    } else {
   55|  20.6M|      v = parent[ances[v]];
   56|  20.6M|    }
   57|  41.2M|  }
   58|  10.0M|  return depth[u] < depth[v] ? u : v;
                                             ^3.50M^6.49M
  ------------------
  |  Branch (58:10): [True: 35.08%, False: 64.92%]
  ------------------
   59|  10.0M|}
   60|       |
   61|  7.10M|def jump(int u, int d) -> int {
   62|  15.0M|  while (depth[ances[u]] > d) {
  ------------------
  |  Branch (62:10): [True: 52.80%, False: 47.20%]
  ------------------
   63|  7.95M|    u = parent[ances[u]];
   64|  7.95M|  }
   65|  7.10M|  return id2node[node2id[u] - depth[u] + d];
   66|  7.10M|}
   67|       |
   68|       |} // namespace
   69|       |
   70|     21|int main() {
   71|     21|  rd rd;
   72|     21|  wt wt;
   73|     21|  int n = rd.uh();
   74|     21|  int q = rd.uh();
   75|     21|#ifdef LOCAL
   76|     21|  id = 0;
   77|     21|  std::memset(head, 0, 4 * n);
   78|     21|  std::memset(heavy, 0, 4 * n);
   79|     21|  std::memset(parent, 0, 4 * n);
   80|     21|#endif
   81|  6.61M|  for (int i = 1; i < n; ++i) {
                                       ^6.61M
  ------------------
  |  Branch (81:19): [True: 100.00%, False: 0.00%]
  ------------------
   82|  6.61M|    int a = rd.uh();
   83|  6.61M|    int b = rd.uh();
   84|  6.61M|    edge[i * 2 | 0] = {b, head[a]}, head[a] = i * 2 | 0;
   85|  6.61M|    edge[i * 2 | 1] = {a, head[b]}, head[b] = i * 2 | 1;
   86|  6.61M|  }
   87|     21|  build_step_1(0, 0, -1);
   88|     21|  build_step_2(0, 0, -1);
   89|  10.0M|  while (q--) {
  ------------------
  |  Branch (89:10): [True: 100.00%, False: 0.00%]
  ------------------
   90|  10.0M|    int u = rd.uh();
   91|  10.0M|    int v = rd.uh();
   92|  10.0M|    int k = rd.uh();
   93|  10.0M|    int w = lca(u, v);
   94|  10.0M|    int du = depth[u];
   95|  10.0M|    int dv = depth[v];
   96|  10.0M|    int dw = depth[w];
   97|  10.0M|    if (k <= du - dw) {
  ------------------
  |  Branch (97:9): [True: 38.72%, False: 61.28%]
  ------------------
   98|  3.87M|      int x = jump(u, du - k);
   99|  3.87M|      wt.uw(x);
  100|  6.12M|    } else if (k <= du + dv - dw - dw) {
  ------------------
  |  Branch (100:16): [True: 52.82%, False: 47.18%]
  ------------------
  101|  3.23M|      int x = jump(v, dw + dw - du + k);
  102|  3.23M|      wt.uw(x);
  103|  3.23M|    } else {
  104|  2.89M|      wt.puts(" -1", 3);
  105|  2.89M|    }
  106|  10.0M|  }
  107|     21|  return 0;
  108|     21|}