2872. Maximum Number of K-Divisible Components Hard
1/**
2 * [2872] Maximum Number of K-Divisible Components
3 *
4 * There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and 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 * You are also given a 0-indexed integer array values of length n, where values[i] is the value associated with the i^th node, and an integer k.
6 * A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by k, where the value of a connected component is the sum of the values of its nodes.
7 * Return the maximum number of components in any valid split.
8 *
9 * <strong class="example">Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg" style="width: 1024px; height: 453px;" />
11 * Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
12 * Output: 2
13 * Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because:
14 * - The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.
15 * - The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.
16 * It can be shown that no other valid split has more than 2 connected components.
17 * <strong class="example">Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg" style="width: 999px; height: 338px;" />
19 * Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
20 * Output: 3
21 * Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:
22 * - The value of the component containing node 0 is values[0] = 3.
23 * - The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.
24 * - The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.
25 * It can be shown that no other valid split has more than 3 connected components.
26 *
27 *
28 * Constraints:
29 *
30 * 1 <= n <= 3 * 10^4
31 * edges.length == n - 1
32 * edges[i].length == 2
33 * 0 <= ai, bi < n
34 * values.length == n
35 * 0 <= values[i] <= 10^9
36 * 1 <= k <= 10^9
37 * Sum of values is divisible by k.
38 * The input is generated such that edges represents a valid tree.
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/maximum-number-of-k-divisible-components/
44// discuss: https://leetcode.com/problems/maximum-number-of-k-divisible-components/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, k: i32) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_2872() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.