<p>Alice has an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. The tree is represented as a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>

<p>Alice wants Bob to find the root of the tree. She allows Bob to make several <strong>guesses</strong> about her tree. In one guess, he does the following:</p>

<ul>
	<li>Chooses two <strong>distinct</strong> integers <code>u</code> and <code>v</code> such that there exists an edge <code>[u, v]</code> in the tree.</li>
	<li>He tells Alice that <code>u</code> is the <strong>parent</strong> of <code>v</code> in the tree.</li>
</ul>

<p>Bob&#39;s guesses are represented by a 2D integer array <code>guesses</code> where <code>guesses[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates Bob guessed <code>u<sub>j</sub></code> to be the parent of <code>v<sub>j</sub></code>.</p>

<p>Alice being lazy, does not reply to each of Bob&#39;s guesses, but just says that <strong>at least</strong> <code>k</code> of his guesses are <code>true</code>.</p>

<p>Given the 2D integer arrays <code>edges</code>, <code>guesses</code> and the integer <code>k</code>, return <em>the <strong>number of possible nodes</strong> that can be the root of Alice&#39;s tree</em>. If there is no such tree, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" /></p>

<pre>
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 
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.

</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" /></p>

<pre>
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> 
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. 

</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>edges.length == n - 1</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= guesses.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> &lt;= n - 1</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
	<li><code>edges</code> represents a valid tree.</li>
	<li><code>guesses[j]</code> is an edge of the tree.</li>
	<li><code>guesses</code> is unique.</li>
	<li><code>0 &lt;= k &lt;= guesses.length</code></li>
</ul>

class Solution:
    def rootCount(self, edges: List[List[int]], guesses: List[List[int]], k: int) -> int:
        N = len(edges)+1 # number of nodes

        # prepare an adjacency list by considering edges in both possible directions
        edge_dict = {i: set() for i in range(N)}

        for u,v in edges:
            edge_dict[u].add(v)
            edge_dict[v].add(u)
        
        # convert guesses to a set for constant time look up
        guess_set = set()
        for u, v in guesses:
            guess_set.add((u,v)) # add the guessed edge as an tuple

        # DFS method to make a tree from the list of edges and the given root
        # also, count the number of valid guesses while constructing the tree
        def make_tree(root, tree, visited) -> int:
            if root in visited:
                return correct_guesses
            visited.add(root)

            correct_guesses = 0

            # add each possible edge where root can be a parent
            for n in edge_dict[root]:
                # if n not in visited means n has not been yet added as a root
                if n not in visited:
                    tree.append((root, n)) # add the edge as an tuple
                    if (root,n) in guess_set:
                        correct_guesses += 1
                    correct_guesses += make_tree(n, tree, visited)
            
            return correct_guesses
        
        # now, construct a tree by choosing a root node at random
        tree, visited = [], set()

        first_root = 1 # choose any node
        valid_guesses = make_tree(first_root, tree, visited)

        ans = 0

        # reroot the tree and adjust the valid guesses count
        # (for reroot, we will recursively inverse the direction of each edge in the original tree
        # and adjust the count accordingly)
        # for each guess count >= k, we update the ans
        def reroot(node, parent, visited, correct_guesses):
            nonlocal ans

            if node in visited:
                return
            visited.add(node)

            if (parent, node) in guess_set: # if the original pair was in the guess, decrease the count (as we need to inverse the edge)
                correct_guesses -= 1
            
            if (node, parent) in guess_set:
                correct_guesses += 1

            if correct_guesses >= k:
                ans += 1
            
            for neighbour in edge_dict[node]:
                reroot(neighbour, node, visited, correct_guesses)
        

        visited = set()
        reroot(first_root, -1, visited, valid_guesses)
        
        return ans
1805