#include #include #include "testlib.h" using namespace std; struct Edge { int u; int v; int cost; Edge(int a, int b, int c) : u(a), v(b), cost(c) { } }; using Graph = vector>; vector depth; vector subtree_size; vector seen; void dfs(const Graph& G, int v, int p, int d) { depth[v] = d; seen[v] = true; for (auto nv : G[v]) { if (nv.v == p) continue; dfs(G, nv.v, v, d + 1); } subtree_size[v] = 1; for (auto c : G[v]) { if (c.v == p) continue; subtree_size[v] += subtree_size[c.v]; } } int main() { registerValidation(); long long N = inf.readInt(2, 100000, "N"); inf.readSpace(); long long Q = inf.readInt(1, 100000, "Q"); inf.readEoln(); Graph G(N); for (int i = 0; i < N - 1; i++) { int a = inf.readInt(1, N, "a"); inf.readSpace(); int b = inf.readInt(1, N, "b"); inf.readEoln(); G[a - 1].emplace_back(a - 1, b - 1, 1); G[b - 1].emplace_back(b - 1, a - 1, 1); } depth.assign(N, 0); subtree_size.assign(N, 0); seen.assign(N, false); dfs(G, 0, -1, 0); for (int i = 0; i < N; i++) { if (seen[i] == false) cout << -1 << endl; } long long ans = 0; for (int i = 0; i < Q; i++) { long long p = inf.readInt(1, N, "p"); inf.readSpace(); long long x = inf.readInt(1, 100000000, "x"); inf.readEoln(); ans += subtree_size[p - 1] * x; cout << ans << endl; } inf.readEof(); }