2581. Count Number of Possible Root Nodes Hard

@problem@discussion
#Array#Hash Table#Dynamic Programming#Tree#Depth-First Search



1/**
2 * [2581] Count Number of Possible Root Nodes
3 *
4 * 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.
5 * 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:
6 * 
7 * 	Chooses two distinct integers u and v such that there exists an edge [u, v] in the tree.
8 * 	He tells Alice that u is the parent of v in the tree.
9 * 
10 * 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.
11 * Alice being lazy, does not reply to each of Bob's guesses, but just says that at least k of his guesses are true.
12 * 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.
13 *  
14 * <strong class="example">Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" />
16 * 
17 * Input: edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
18 * Output: 3
19 * Explanation: 
20 * Root = 0, correct guesses = [1,3], [0,1], [2,4]
21 * Root = 1, correct guesses = [1,3], [1,0], [2,4]
22 * Root = 2, correct guesses = [1,3], [1,0], [2,4]
23 * Root = 3, correct guesses = [1,0], [2,4]
24 * Root = 4, correct guesses = [1,3], [1,0]
25 * Considering 0, 1, or 2 as root node leads to 3 correct guesses.
26 * 
27 * <strong class="example">Example 2:
28 * <img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" />
29 * 
30 * Input: edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
31 * Output: 5
32 * Explanation: 
33 * Root = 0, correct guesses = [3,4]
34 * Root = 1, correct guesses = [1,0], [3,4]
35 * Root = 2, correct guesses = [1,0], [2,1], [3,4]
36 * Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]
37 * Root = 4, correct guesses = [1,0], [2,1], [3,2]
38 * Considering any node as root will give at least 1 correct guess. 
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	edges.length == n - 1
44 * 	2 <= n <= 10^5
45 * 	1 <= guesses.length <= 10^5
46 * 	0 <= ai, bi, uj, vj <= n - 1
47 * 	ai != bi
48 * 	uj != vj
49 * 	edges represents a valid tree.
50 * 	guesses[j] is an edge of the tree.
51 * 	guesses is unique.
52 * 	0 <= k <= guesses.length
53 * 
54 */
55pub struct Solution {}
56
57// problem: https://leetcode.com/problems/count-number-of-possible-root-nodes/
58// discuss: https://leetcode.com/problems/count-number-of-possible-root-nodes/discuss/?currentPage=1&orderBy=most_votes&query=
59
60// submission codes start here
61
62impl Solution {
63    pub fn root_count(edges: Vec<Vec<i32>>, guesses: Vec<Vec<i32>>, k: i32) -> i32 {
64        0
65    }
66}
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_2581() {
76    }
77}
78


Back
© 2025 bowen.ge All Rights Reserved.