From 9563592af471e7174b6f6c47b2367b8858c314b1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 18 Nov 2025 22:10:23 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2872 --- .../README.md | 39 ++++++++++++++++- .../README_EN.md | 43 +++++++++++++++++-- .../Solution.cpp | 4 +- .../Solution.rs | 30 +++++++++++++ 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.rs diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md index c8902b95e148f..25e278ea49c55 100644 --- a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md @@ -79,7 +79,7 @@ tags: ### 方法一:DFS -我们注意到,题目保证了整棵树的节点值之和可以被 $k$ 整除,因此,如果我们删除一棵元素和能被 $k$ 整除的边,那么剩下的每个连通块的节点值之和也一定可以被 $k$ 整除。 +我们注意到,题目保证了整棵树的节点值之和可以被 $k$ 整除,因此,如果我们删除一棵元素和能被 $k$ 整除的子树,那么剩下的每个连通块的节点值之和也一定可以被 $k$ 整除。 因此,我们可以使用深度优先搜索的方法,从根节点开始遍历整棵树,对于每个节点,我们计算其子树中所有节点值之和,如果该和能被 $k$ 整除,那么我们就将答案加一。 @@ -161,7 +161,7 @@ public: g[a].push_back(b); g[b].push_back(a); } - function dfs = [&](int i, int fa) { + auto dfs = [&](this auto&& dfs, int i, int fa) -> long long { long long s = values[i]; for (int j : g[i]) { if (j != fa) { @@ -237,6 +237,41 @@ function maxKDivisibleComponents( } ``` +#### Rust + +```rust +impl Solution { + pub fn max_k_divisible_components(n: i32, edges: Vec>, values: Vec, k: i32) -> i32 { + let n = n as usize; + let mut g = vec![vec![]; n]; + for e in edges { + let a = e[0] as usize; + let b = e[1] as usize; + g[a].push(b); + g[b].push(a); + } + + let mut ans = 0; + + fn dfs(i: usize, fa: i32, g: &Vec>, values: &Vec, k: i32, ans: &mut i32) -> i64 { + let mut s = values[i] as i64; + for &j in &g[i] { + if j as i32 != fa { + s += dfs(j, i as i32, g, values, k, ans); + } + } + if s % k as i64 == 0 { + *ans += 1; + } + s + } + + dfs(0, -1, &g, &values, k, &mut ans); + ans + } +} +``` + diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md index d910363073487..2d8560aef5357 100644 --- a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md @@ -73,11 +73,11 @@ It can be shown that no other valid split has more than 3 connected components. ### Solution 1: DFS -We notice that the problem guarantees that the sum of the node values in the entire tree can be divided by $k$, so if we remove an edge whose weight is divisible by $k$, then the sum of the node values in each connected component that remains can also be divided by $k$. +We note that the problem guarantees the sum of all node values in the entire tree is divisible by $k$. Therefore, if we remove a subtree whose sum of elements is divisible by $k$, the sum of node values in each of the remaining connected components must also be divisible by $k$. -Therefore, we can use depth-first search to traverse the entire tree starting from the root node. For each node, we calculate the sum of all node values in its subtree. If this sum can be divided by k, then we increment the answer by one. +Thus, we can use a depth-first search approach, starting from the root node to traverse the entire tree. For each node, we calculate the sum of all node values in its subtree. If this sum is divisible by $k$, we increment the answer by one. -The time complexity is $O(n)$, and the space complexity is $O(n)$, where n is the number of nodes in the tree. +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree. @@ -155,7 +155,7 @@ public: g[a].push_back(b); g[b].push_back(a); } - function dfs = [&](int i, int fa) { + auto dfs = [&](this auto&& dfs, int i, int fa) -> long long { long long s = values[i]; for (int j : g[i]) { if (j != fa) { @@ -231,6 +231,41 @@ function maxKDivisibleComponents( } ``` +#### Rust + +```rust +impl Solution { + pub fn max_k_divisible_components(n: i32, edges: Vec>, values: Vec, k: i32) -> i32 { + let n = n as usize; + let mut g = vec![vec![]; n]; + for e in edges { + let a = e[0] as usize; + let b = e[1] as usize; + g[a].push(b); + g[b].push(a); + } + + let mut ans = 0; + + fn dfs(i: usize, fa: i32, g: &Vec>, values: &Vec, k: i32, ans: &mut i32) -> i64 { + let mut s = values[i] as i64; + for &j in &g[i] { + if j as i32 != fa { + s += dfs(j, i as i32, g, values, k, ans); + } + } + if s % k as i64 == 0 { + *ans += 1; + } + s + } + + dfs(0, -1, &g, &values, k, &mut ans); + ans + } +} +``` + diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.cpp b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.cpp index 7e59a2047f949..ab9b033b9e2fa 100644 --- a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.cpp +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.cpp @@ -8,7 +8,7 @@ class Solution { g[a].push_back(b); g[b].push_back(a); } - function dfs = [&](int i, int fa) { + auto dfs = [&](this auto&& dfs, int i, int fa) -> long long { long long s = values[i]; for (int j : g[i]) { if (j != fa) { @@ -21,4 +21,4 @@ class Solution { dfs(0, -1); return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.rs b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.rs new file mode 100644 index 0000000000000..85f4ffdeefa2d --- /dev/null +++ b/solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.rs @@ -0,0 +1,30 @@ +impl Solution { + pub fn max_k_divisible_components(n: i32, edges: Vec>, values: Vec, k: i32) -> i32 { + let n = n as usize; + let mut g = vec![vec![]; n]; + for e in edges { + let a = e[0] as usize; + let b = e[1] as usize; + g[a].push(b); + g[b].push(a); + } + + let mut ans = 0; + + fn dfs(i: usize, fa: i32, g: &Vec>, values: &Vec, k: i32, ans: &mut i32) -> i64 { + let mut s = values[i] as i64; + for &j in &g[i] { + if j as i32 != fa { + s += dfs(j, i as i32, g, values, k, ans); + } + } + if s % k as i64 == 0 { + *ans += 1; + } + s + } + + dfs(0, -1, &g, &values, k, &mut ans); + ans + } +}