Skip to content

Commit 9563592

Browse files
committed
feat: add solutions to lc problem: No.2872
1 parent 06d2d77 commit 9563592

File tree

4 files changed

+108
-8
lines changed

4 files changed

+108
-8
lines changed

solution/2800-2899/2872.Maximum Number of K-Divisible Components/README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tags:
7979

8080
### 方法一:DFS
8181

82-
我们注意到,题目保证了整棵树的节点值之和可以被 $k$ 整除,因此,如果我们删除一棵元素和能被 $k$ 整除的边,那么剩下的每个连通块的节点值之和也一定可以被 $k$ 整除。
82+
我们注意到,题目保证了整棵树的节点值之和可以被 $k$ 整除,因此,如果我们删除一棵元素和能被 $k$ 整除的子树,那么剩下的每个连通块的节点值之和也一定可以被 $k$ 整除。
8383

8484
因此,我们可以使用深度优先搜索的方法,从根节点开始遍历整棵树,对于每个节点,我们计算其子树中所有节点值之和,如果该和能被 $k$ 整除,那么我们就将答案加一。
8585

@@ -161,7 +161,7 @@ public:
161161
g[a].push_back(b);
162162
g[b].push_back(a);
163163
}
164-
function<long long(int, int)> dfs = [&](int i, int fa) {
164+
auto dfs = [&](this auto&& dfs, int i, int fa) -> long long {
165165
long long s = values[i];
166166
for (int j : g[i]) {
167167
if (j != fa) {
@@ -237,6 +237,41 @@ function maxKDivisibleComponents(
237237
}
238238
```
239239

240+
#### Rust
241+
242+
```rust
243+
impl Solution {
244+
pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, k: i32) -> i32 {
245+
let n = n as usize;
246+
let mut g = vec![vec![]; n];
247+
for e in edges {
248+
let a = e[0] as usize;
249+
let b = e[1] as usize;
250+
g[a].push(b);
251+
g[b].push(a);
252+
}
253+
254+
let mut ans = 0;
255+
256+
fn dfs(i: usize, fa: i32, g: &Vec<Vec<usize>>, values: &Vec<i32>, k: i32, ans: &mut i32) -> i64 {
257+
let mut s = values[i] as i64;
258+
for &j in &g[i] {
259+
if j as i32 != fa {
260+
s += dfs(j, i as i32, g, values, k, ans);
261+
}
262+
}
263+
if s % k as i64 == 0 {
264+
*ans += 1;
265+
}
266+
s
267+
}
268+
269+
dfs(0, -1, &g, &values, k, &mut ans);
270+
ans
271+
}
272+
}
273+
```
274+
240275
<!-- tabs:end -->
241276

242277
<!-- solution:end -->

solution/2800-2899/2872.Maximum Number of K-Divisible Components/README_EN.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ It can be shown that no other valid split has more than 3 connected components.
7373

7474
### Solution 1: DFS
7575

76-
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$.
76+
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$.
7777

78-
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.
78+
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.
7979

80-
The time complexity is $O(n)$, and the space complexity is $O(n)$, where n is the number of nodes in the tree.
80+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.
8181

8282
<!-- tabs:start -->
8383

@@ -155,7 +155,7 @@ public:
155155
g[a].push_back(b);
156156
g[b].push_back(a);
157157
}
158-
function<long long(int, int)> dfs = [&](int i, int fa) {
158+
auto dfs = [&](this auto&& dfs, int i, int fa) -> long long {
159159
long long s = values[i];
160160
for (int j : g[i]) {
161161
if (j != fa) {
@@ -231,6 +231,41 @@ function maxKDivisibleComponents(
231231
}
232232
```
233233

234+
#### Rust
235+
236+
```rust
237+
impl Solution {
238+
pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, k: i32) -> i32 {
239+
let n = n as usize;
240+
let mut g = vec![vec![]; n];
241+
for e in edges {
242+
let a = e[0] as usize;
243+
let b = e[1] as usize;
244+
g[a].push(b);
245+
g[b].push(a);
246+
}
247+
248+
let mut ans = 0;
249+
250+
fn dfs(i: usize, fa: i32, g: &Vec<Vec<usize>>, values: &Vec<i32>, k: i32, ans: &mut i32) -> i64 {
251+
let mut s = values[i] as i64;
252+
for &j in &g[i] {
253+
if j as i32 != fa {
254+
s += dfs(j, i as i32, g, values, k, ans);
255+
}
256+
}
257+
if s % k as i64 == 0 {
258+
*ans += 1;
259+
}
260+
s
261+
}
262+
263+
dfs(0, -1, &g, &values, k, &mut ans);
264+
ans
265+
}
266+
}
267+
```
268+
234269
<!-- tabs:end -->
235270

236271
<!-- solution:end -->

solution/2800-2899/2872.Maximum Number of K-Divisible Components/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Solution {
88
g[a].push_back(b);
99
g[b].push_back(a);
1010
}
11-
function<long long(int, int)> dfs = [&](int i, int fa) {
11+
auto dfs = [&](this auto&& dfs, int i, int fa) -> long long {
1212
long long s = values[i];
1313
for (int j : g[i]) {
1414
if (j != fa) {
@@ -21,4 +21,4 @@ class Solution {
2121
dfs(0, -1);
2222
return ans;
2323
}
24-
};
24+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, k: i32) -> i32 {
3+
let n = n as usize;
4+
let mut g = vec![vec![]; n];
5+
for e in edges {
6+
let a = e[0] as usize;
7+
let b = e[1] as usize;
8+
g[a].push(b);
9+
g[b].push(a);
10+
}
11+
12+
let mut ans = 0;
13+
14+
fn dfs(i: usize, fa: i32, g: &Vec<Vec<usize>>, values: &Vec<i32>, k: i32, ans: &mut i32) -> i64 {
15+
let mut s = values[i] as i64;
16+
for &j in &g[i] {
17+
if j as i32 != fa {
18+
s += dfs(j, i as i32, g, values, k, ans);
19+
}
20+
}
21+
if s % k as i64 == 0 {
22+
*ans += 1;
23+
}
24+
s
25+
}
26+
27+
dfs(0, -1, &g, &values, k, &mut ans);
28+
ans
29+
}
30+
}

0 commit comments

Comments
 (0)