Count Number of Possible Root Nodes - Problem

Imagine Alice has a beautiful undirected tree with n nodes labeled from 0 to n-1. The tree is represented as a 2D array edges where each edges[i] = [ai, bi] indicates there's an edge connecting nodes ai and bi.

Alice wants Bob to discover the root of her tree. She allows Bob to make educated guesses about parent-child relationships. In each guess, Bob:

  • Chooses two connected nodes u and v (where an edge exists between them)
  • Guesses that u is the parent of v in the rooted tree

Bob's guesses are represented by array guesses where guesses[j] = [uj, vj] means Bob thinks uj is the parent of vj.

Alice, being busy, doesn't check each guess individually. Instead, she simply tells Bob that at least k of his guesses are correct.

Your mission: Given the tree structure (edges), Bob's guesses (guesses), and the minimum correct guesses (k), determine how many different nodes could potentially be the root of Alice's tree.

Input & Output

example_1.py β€” Basic Tree
$ Input: edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
β€Ί Output: 3
πŸ’‘ Note: We have a tree with 5 nodes. Bob made 4 guesses about parent-child relationships. For this tree, nodes 0, 1, and 4 can be valid roots because each allows at least 3 correct guesses.
example_2.py β€” Simple Path
$ Input: edges = [[0,1],[1,2],[2,3]], guesses = [[0,1],[1,2],[2,3]], k = 2
β€Ί Output: 2
πŸ’‘ Note: This is a simple path: 0-1-2-3. Only nodes 0 and 1 can be roots with at least 2 correct guesses. When 0 is root: (0β†’1), (1β†’2), (2β†’3) are all correct. When 1 is root: (1β†’2), (2β†’3) are correct.
example_3.py β€” High Threshold
$ Input: edges = [[0,1],[0,2]], guesses = [[0,1],[2,0]], k = 3
β€Ί Output: 0
πŸ’‘ Note: This is a star with center 0. Bob guessed (0β†’1) and (2β†’0). No root can have 3 or more correct guesses since there are only 2 edges total, so the answer is 0.

Constraints

  • n == edges.length + 1
  • 2 ≀ n ≀ 105
  • 1 ≀ guesses.length ≀ 105
  • 0 ≀ ai, bi < n
  • ai != bi
  • edges represents a valid tree
  • guesses[i] = [ui, vi] where there exists an edge between ui and vi
  • 0 ≀ k ≀ guesses.length

Visualization

Tap to expand
πŸ•΅οΈ Detective's Family Tree InvestigationπŸ‘‘Current LeaderABCDπŸ” Detective's GuessesπŸ‘‘ β†’ A βœ…πŸ‘‘ β†’ B βœ…A β†’ C βœ…B β†’ A ❌A β†’ D βœ…Score: 4/5 βœ“πŸ’‘ Key Detective InsightWhen we change the family leader from Parent to Child:β€’ We lose the "Parent β†’ Child" relationship βŒβ€’ We might gain "Child β†’ Parent" if detective guessed it βœ…Smart Update: New Score = Old Score - Lost + Gained
Understanding the Visualization
1
Build Family Tree
Create the family connections from the edges
2
Test Leadership
For each potential family head, see how many guesses match reality
3
Efficient Transition
When changing leaders, smartly update the correct guess count
4
Count Valid Leaders
Count how many people could lead with at least k correct guesses
Key Takeaway
🎯 Key Insight: Use tree rerooting to efficiently test all possible roots by updating the correct guess count incrementally rather than recalculating from scratch.
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
38.5K Views
Medium Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen