voiddfs1(int u, int fa) { // unordered_set<int> son; for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (v == fa) continue; if (count(u, v)) ++ans[0]; dfs1(v, u); } } voiddfs2(int u, int fa) { for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (v == fa) continue; ans[v] = ans[u]; if (count(u, v)) ans[v]--; if (count(v, u)) ans[v]++; dfs2(v, u); } } introotCount(vector<vector<int>> &edges, vector<vector<int>> &guesses, int k) { int n = edges.size() + 1; g.resize(n); ans.resize(n); for (auto &edge : edges) { int u = edge[0], v = edge[1]; g[u].emplace_back(v); g[v].emplace_back(u); } for (auto &edge : guesses) { int u = edge[0], v = edge[1]; insert(u, v); } // 先假设 0 为根,遍历一遍,找到几个为 true 的 dfs1(0, -1); dfs2(0, -1); int cnt = 0; for (int i = 0; i < n; ++i) { if (ans[i] >= k) ++cnt; } return cnt; } };