#include #include #include #include #include #include #include #include #include using namespace std; using i64 = int64_t; using u64 = uint64_t; using i32 = int32_t; using u32 = uint32_t; #define REP(i, s, t) for (i32 i = (s); i < (t); ++i) struct HLD { const i32 size; vector> graph; vector left; vector right; vector vertex; vector parent; vector path_root; HLD(const i32 size) : size(size), graph(size), left(size), right(size), vertex(size), parent(size, -1), path_root(size) {} void add_edge(const i32 a, const i32 b) { assert(0 <= min(a, b) && max(a, b) < size); graph[a].emplace_back(b); graph[b].emplace_back(a); } void build(const i32 root) { dfs_size(root, -1); i32 id = 0; path_root[root] = root; dfs_ord(root, id); } i32 dfs_size(const i32 v, const i32 p) { i32 s = 1; i32 max = 0; i32 max_pos = -1; for (i32 i = 0; i < (i32)graph[v].size(); ++i) { const i32 u = graph[v][i]; if (u == p) continue; parent[u] = v; const i32 c = dfs_size(u, v); if (c > max) { max = c; max_pos = i; } s += c; } if (max_pos >= 0) { swap(graph[v][max_pos], graph[v][0]); } return s; } void dfs_ord(const i32 v, i32 &id) { left[v] = id; vertex[id] = v; id += 1; for (i32 i = 0; i < (i32)graph[v].size(); ++i) { const i32 u = graph[v][i]; if (u == parent[v]) continue; if (i == 0) { path_root[u] = path_root[v]; dfs_ord(u, id); } else { path_root[u] = u; dfs_ord(u, id); } } right[v] = id; } pair subtree(const i32 v) { assert(v < size); return make_pair(left[v], right[v]); } i32 next(const i32 s, i32 t) { assert(0 <= min(s, t) && max(s, t) < size && s != t); const auto [x, y] = subtree(s); const auto [z, w] = subtree(t); if (!(x <= z && w <= y)) { return parent[s]; } i32 pre = t; while (path_root[s] != path_root[t]) { pre = path_root[t]; t = parent[pre]; } return t == s ? pre : vertex[x + 1]; } vector> path(i32 s, i32 t) { assert(0 <= min(s, t) && max(s, t) < size && s != t); static pair a[24]; static pair b[24]; i32 x = 0; i32 y = 0; while (path_root[s] != path_root[t]) { if (left[s] < left[t]) { const i32 p = path_root[t]; b[y++] = make_pair(left[p], left[t] + 1); t = parent[p]; } else { const i32 p = path_root[s]; a[x++] = make_pair(left[p], left[s] + 1); s = parent[p]; } } if (left[s] <= left[t]) { b[y++] = make_pair(left[s], left[t] + 1); } else { a[x++] = make_pair(left[t], left[s] + 1); } vector> res(x + y); for (i32 i = 0; i < x; ++i) { res[i] = a[i]; } for (i32 i = 0; i < y; ++i) { res[x + i] = b[y - 1 - i]; } return res; } }; i32 read_int(void) { i32 v; cin >> v; return v; } void run(void) { const i32 n = read_int() + 1; const i32 q = read_int(); HLD hld(n); hld.add_edge(0, 1); vector> graph(n + 1); REP(i, 2, n) { const i32 a = read_int(); const i32 b = read_int(); hld.add_edge(a, b); graph[a].emplace_back(b); graph[b].emplace_back(a); } hld.build(0); vector state(n, 0); REP(i, 0, q) { const i32 u = read_int(); const i32 r = read_int(); const i32 v = read_int(); state[u] ^= 1; i64 ans = 0; auto dfs = [&](auto self, i32 v, i32 p) -> i64 { i64 s = state[v]; for (auto u: graph[v]) { if (u != p) { i64 c = self(self, u, v); ans += (i64)v * s * c; s += c; } } return s; }; i32 p = r == v ? -1 : hld.next(v, r); dfs(dfs, v, p); cout << ans << "\n"; } } int main(void) { cin.tie(nullptr); ios::sync_with_stdio(false); run(); return 0; }