# @lc code=start class Solution: def rootCount(self, edges: List[List[int]], guesses: List[List[int]], k: int) -> int: g = [[] for i in range(len(edges) + 1)] for a in edges: g[a[0]].append(a[1]) g[a[1]].append(a[0]) guesse = {(a[0], a[1]) for a in guesses} ans = 0 base = 0 def dfs(rt, fa):
if (fa, rt) in guesse: nonlocal base base += 1 for nx in g[rt]: if nx == fa: continue dfs(nx, rt)
dfs(0, -1) # print(base)
def check(rt, fa, now): nonlocal ans if (now >= k): ans += 1 for nx in g[rt]: if nx == fa: continue tmp = now if (rt, nx) in guesse: tmp -= 1 if (nx, rt) in guesse: tmp += 1 check(nx, rt, tmp)