void dfs(str& s, int u) { vis[u] = true; for (auto child: v[u]) if (!vis[child]) { str* cs = new(str); dfs(*cs, child); merge(s, *cs); delete(cs); } s.insert(c[u], 1); for (auto query: q[u]) { int pos = query.second, k = query.first; result[pos] = s.query(k); } }
void merge(str& s1, str& s2) { if (s1.m.size() < s2.m.size()) swap(s1, s2); for (auto it: s2.m) s1.insert(it.first, it.second); }