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] = [ai, bi] indicates that there is an edge between nodes ai and bi 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 u and v such that there exists an edge [u, v] in the tree.
He tells Alice that u is the parent of v in the tree.
Bob's guesses are represented by a 2D integer array guesses where guesses[j] = [uj, vj] indicates Bob guessed uj to be the parent of vj.
Alice being lazy, does not reply to each of Bob's guesses, but just says that at leastk 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.
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 - 1
2 <= n <= 105
1 <= guesses.length <= 105
0 <= ai, bi, uj, vj <= n - 1
ai != bi
uj != vj
edges represents a valid tree.
guesses[j] is an edge of the tree.
guesses is unique.
0 <= k <= guesses.length
Solutions
Solution 1: Tree DP (change root)
Thinking
The root is unknown; guesses are directed edges. A node is valid if, when it is the root, at least \(k\) guesses match. A DFS from every root is \(O(n^2)\).
First count matches \(\textit{cnt}\) with root \(0\). Moving the root to a neighbour \(j\) drops edge \((i,j)\) as a parent-to-child and adds \((j,i)\) if guessed. A second DFS visits every root with this \(O(1)\) update.
First, we traverse the given edge set \(\textit{edges}\) and convert it to an adjacency list \(g\) where \(g[i]\) represents the adjacent nodes of node \(i\). Use a hash map \(gs\) to record the given guess set \(\textit{guesses}\).
Then, we start from node \(0\) and perform a DFS to count the number of edges in \(\textit{guesses}\) among all the nodes that can be reached from node \(0\). We use the variable \(\textit{cnt}\) to record this number.
Next, we start from node \(0\) and perform a DFS to count the number of edges in \(\textit{guesses}\) in each tree with \(0\) as the root. If the number is greater than or equal to \(k\), it means that this node is a possible root node, and we add \(1\) to the answer.
Therefore, the problem becomes to count the number of edges in \(\textit{guesses}\) in each tree with each node as the root. We already know that there are \(\textit{cnt}\) edges in \(\textit{guesses}\) among all the nodes that can be reached from node \(0\). We can maintain this value by adding or subtracting the current edge in \(\textit{guesses}\) in DFS.
Assume that we are currently traversing node \(i\) and \(\textit{cnt}\) represents the number of edges in \(\textit{guesses}\) with \(i\) as the root node. Then, for each adjacent node \(j\) of \(i\), we need to calculate the number of edges in \(\textit{guesses}\) with \(j\) as the root node. If \((i, j)\) is in \(\textit{guesses}\), then there is no edge \((i, j)\) in the tree with \(j\) as the root node, so \(\textit{cnt}\) should decrease by \(1\). If \((j, i)\) is in \(\textit{guesses}\), then there is an extra edge \((i, j)\) in the tree with \(j\) as the root node, so \(\textit{cnt}\) should increase by \(1\). That is, \(f[j] = f[i] + (j, i) \in \textit{guesses} - (i, j) \in \textit{guesses}\). Where \(f[i]\) represents the number of edges in \(\textit{guesses}\) with \(i\) as the root node.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) are the lengths of \(\textit{edges}\) and \(\textit{guesses}\) respectively.
The root is unknown, and each guess is a directed edge. A node is valid when at least \(k\) guesses match after it becomes the root. With \(n\le 10^5\), a separate walk from every root is quadratic, and recursion along a chain overflows Python once the chain reaches length \(1000\). Moving the root across one edge changes only that edge: from parent \(i\) to child \(j\), \((i,j)\) is no longer parent-to-child and \((j,i)\) becomes parent-to-child. The first stack therefore starts at \(0\) and adds each guess whose direction matches the parent-child edge, producing \(\textit{cnt}\). The second stack stores the adjusted count in the frame. A popped node is counted when that value is at least \(k\), and the child receives \(\textit{cnt}-(i,j)+(j,i)\).
Turn \(\textit{edges}\) into an adjacency list \(g\), and store \(\textit{guesses}\) in a hash map \(gs\). An explicit stack walks the tree from node \(0\). When the walk moves to a child \(j\), add the multiplicity of \((i,j)\) in \(gs\) to \(\textit{cnt}\). After this walk, \(\textit{cnt}\) is the number of correct guesses with root \(0\).
The second stack reroots the tree. Each frame stores a node, its parent, and the number of correct guesses when that node is the root. It starts with \((0,-1,\textit{cnt})\). When node \(i\) is popped, add \(1\) to the answer if the stored count is at least \(k\). For a neighbor \(j\), \((i,j)\) is no longer parent-to-child and \((j,i)\) becomes parent-to-child, so the count passed to \(j\) is \(c-gs[(i,j)]+gs[(j,i)]\).
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) are the lengths of \(\textit{edges}\) and \(\textit{guesses}\) respectively.