This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/jump_on_tree"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/heavy_light_decomposition.hpp"
int main(void) {
int n, q;
cin >> n >> q;
Graph<int> g(n);
rep(i, 0, n - 1) {
int u, v;
cin >> u >> v;
g.add_edge(u, v);
}
HeavyLightDecomposition<int> hld(g);
while(q--) {
int s, t, i;
cin >> s >> t >> i;
int dist = hld.dist(s, t);
if(i > dist) {
cout << -1 << '\n';
continue;
}
int l = hld.lca(s, t);
int d1 = hld.dist(s, l);
if(i <= d1) {
cout << hld.la(s, i) << '\n';
} else {
cout << hld.la(t, dist - i) << '\n';
}
}
}
#line 1 "verify/library_checker/tree/jump_on_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/jump_on_tree"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
int from, to;
T cost;
int idx;
Edge()
: from(-1), to(-1), cost(-1), idx(-1) {}
Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int() const {
return to;
}
};
template <typename T>
struct Graph {
Graph(const int N)
: n(N), es(0), g(N) {}
int size() const {
return n;
}
int edge_size() const {
return es;
}
void add_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void add_directed_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es++);
}
inline vector<Edge<T>>& operator[](const int& k) {
assert(0 <= k and k < n);
return g[k];
}
inline const vector<Edge<T>>& operator[](const int& k) const {
assert(0 <= k and k < n);
return g[k];
}
private:
int n, es;
vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/heavy_light_decomposition.hpp"
template <typename T>
struct HeavyLightDecomposition {
HeavyLightDecomposition(Graph<T>& _g, const int root = 0)
: g(_g), n(g.size()), id(0), sz(n, 0), dep(n, 0), down(n, -1), up(n, -1), nex(n, root), par(n, -1), rev(n, 0), co(n, 0) {
assert(0 <= root and root < n);
dfs_sz(root);
dfs_hld(root);
}
pair<int, int> idx(const int i) const {
assert(0 <= i and i < n);
return make_pair(down[i], up[i]);
}
int depth(const int v) const {
assert(0 <= v and v < n);
return dep[v];
}
T cost(const int v) const {
assert(0 <= v and v < n);
return co[v];
}
int parent(const int v) const {
assert(0 <= v and v < n);
return par[v];
}
int la(int v, int x) const {
assert(0 <= v and v < n);
assert(x >= 0);
if(x > dep[v]) return -1;
while(true) {
const int u = nex[v];
if(down[v] - x >= down[u]) return rev[down[v] - x];
x -= down[v] - down[u] + 1;
v = par[u];
}
}
int lca(int u, int v) const {
assert(0 <= u and u < n);
assert(0 <= v and v < n);
while(nex[u] != nex[v]) {
if(down[u] < down[v]) swap(u, v);
u = par[nex[u]];
}
return dep[u] < dep[v] ? u : v;
}
int dist(const int u, const int v) const {
assert(0 <= u and u < n);
assert(0 <= v and v < n);
return dep[u] + dep[v] - dep[lca(u, v)] * 2;
}
T length(const int u, const int v) const {
assert(0 <= u and u < n);
assert(0 <= v and v < n);
return co[u] + co[v] - co[lca(u, v)] * 2;
}
template <typename F>
void path_query(const int u, const int v, const bool vertex, const F& f) {
assert(0 <= u and u < n);
assert(0 <= v and v < n);
const int l = lca(u, v);
for(auto&& [a, b] : ascend(u, l)) f(a + 1, b);
if(vertex) f(down[l], down[l] + 1);
for(auto&& [a, b] : descend(l, v)) f(a, b + 1);
}
template <typename F>
void subtree_query(const int v, const bool vertex, const F& f) {
assert(0 <= v and v < n);
f(down[v] + int(!vertex), up[v]);
}
private:
Graph<T>& g;
int n, id;
vector<int> sz, dep, down, up, nex, par, rev;
vector<T> co;
void dfs_sz(const int cur) {
sz[cur] = 1;
for(Edge<T>& edge : g[cur]) {
if(edge.to == par[cur]) {
if(g[cur].size() >= 2 and edge.to == g[cur][0].to) {
swap(g[cur][0], g[cur][1]);
} else {
continue;
}
}
dep[edge.to] = dep[cur] + 1;
co[edge.to] = co[cur] + edge.cost;
par[edge.to] = cur;
dfs_sz(edge.to);
sz[cur] += sz[edge.to];
if(sz[edge.to] > sz[g[cur][0].to]) {
swap(edge, g[cur][0]);
}
}
}
void dfs_hld(const int cur) {
down[cur] = id++;
rev[down[cur]] = cur;
for(const Edge<T>& edge : g[cur]) {
if(edge.to == par[cur]) continue;
nex[edge.to] = (edge.to == g[cur][0].to ? nex[cur] : edge.to);
dfs_hld(edge.to);
}
up[cur] = id;
}
vector<pair<int, int>> ascend(int u, int v) const {
vector<pair<int, int>> res;
while(nex[u] != nex[v]) {
res.emplace_back(down[u], down[nex[u]]);
u = par[nex[u]];
}
if(u != v) res.emplace_back(down[u], down[v] + 1);
return res;
}
vector<pair<int, int>> descend(const int u, const int v) const {
if(u == v) return {};
if(nex[u] == nex[v]) return {{down[u] + 1, down[v]}};
auto res = descend(u, par[nex[v]]);
res.emplace_back(down[nex[v]], down[v]);
return res;
}
};
#line 5 "verify/library_checker/tree/jump_on_tree.test.cpp"
int main(void) {
int n, q;
cin >> n >> q;
Graph<int> g(n);
rep(i, 0, n - 1) {
int u, v;
cin >> u >> v;
g.add_edge(u, v);
}
HeavyLightDecomposition<int> hld(g);
while(q--) {
int s, t, i;
cin >> s >> t >> i;
int dist = hld.dist(s, t);
if(i > dist) {
cout << -1 << '\n';
continue;
}
int l = hld.lca(s, t);
int d1 = hld.dist(s, l);
if(i <= d1) {
cout << hld.la(s, i) << '\n';
} else {
cout << hld.la(t, dist - i) << '\n';
}
}
}