Count Number of Possible Root Nodes Problem
Description
LeetCode Problem 2581.
Alice has an undirected tree with n nodes labeled from 0 to n - 1. The tree is represented as a 2D integer array edges of length n - 1 where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
Alice wants Bob to find the root of the tree. She allows Bob to make several guesses about her tree. In one guess, he does the following:
- Chooses two distinct integers
uandvsuch that there exists an edge[u, v]in the tree. - He tells Alice that
uis the parent ofvin the tree.
Bob's guesses are represented by a 2D integer array guesses where guesses[j] = [u_j, v_j] indicates Bob guessed u_j to be the parent of v_j.
Alice being lazy, does not reply to each of Bob's guesses, but just says that at least k of his guesses are true.
Given the 2D integer arrays edges, guesses and the integer k, return the number of possible nodes that can be the root of Alice's tree. If there is no such tree, return 0.
Example 1:

Input: edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
Output: 3
Explanation:
Root = 0, correct guesses = [1,3], [0,1], [2,4]
Root = 1, correct guesses = [1,3], [1,0], [2,4]
Root = 2, correct guesses = [1,3], [1,0], [2,4]
Root = 3, correct guesses = [1,0], [2,4]
Root = 4, correct guesses = [1,3], [1,0]
Considering 0, 1, or 2 as root node leads to 3 correct guesses.
Example 2:

Input: edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
Output: 5
Explanation:
Root = 0, correct guesses = [3,4]
Root = 1, correct guesses = [1,0], [3,4]
Root = 2, correct guesses = [1,0], [2,1], [3,4]
Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]
Root = 4, correct guesses = [1,0], [2,1], [3,2]
Considering any node as root will give at least 1 correct guess.
Constraints:
edges.length == n - 12 <= n <= 10^51 <= guesses.length <= 10^50 <= a_i, b_i, u_j, v_j <= n - 1a_i != b_iu_j != v_jedgesrepresents a valid tree.guesses[j]is an edge of the tree.guessesis unique.0 <= k <= guesses.length
Solution
Approach: Tree DP (rerooting)
Build an adjacency list g from edges, and store the guesses in a hash map gs (counting how many times each directed pair appears).
First run a DFS from node 0. A guess (u, v) is correct when u is the parent of v in the tree rooted at 0, so this pass counts the correct guesses for root 0 in cnt.
Then reroot. Moving the root from i to its neighbor j flips only the direction of edge (i, j): the guess (i, j) stops being correct (cnt decreases by its count), and the guess (j, i) becomes correct (cnt increases by its count). So a second DFS carries cnt along, undoes the change when returning, and counts every node whose cnt is at least k.
The time complexity is O(n + m) and the space complexity is O(n + m), where n and m are the numbers of edges and guesses.
from collections import Counter, defaultdict
class Solution:
def rootCount(
self, edges: List[List[int]], guesses: List[List[int]], k: int
) -> int:
def dfs1(i, fa):
nonlocal cnt
for j in g[i]:
if j != fa:
cnt += gs[(i, j)]
dfs1(j, i)
def dfs2(i, fa):
nonlocal ans, cnt
ans += cnt >= k
for j in g[i]:
if j != fa:
cnt -= gs[(i, j)]
cnt += gs[(j, i)]
dfs2(j, i)
cnt -= gs[(j, i)]
cnt += gs[(i, j)]
g = defaultdict(list)
for a, b in edges:
g[a].append(b)
g[b].append(a)
gs = Counter((u, v) for u, v in guesses)
cnt = 0
dfs1(0, -1)
ans = 0
dfs2(0, -1)
return ans