Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ tags:

### 方法一:DFS

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

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

Expand Down Expand Up @@ -161,7 +161,7 @@ public:
g[a].push_back(b);
g[b].push_back(a);
}
function<long long(int, int)> 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) {
Expand Down Expand Up @@ -237,6 +237,41 @@ function maxKDivisibleComponents(
}
```

#### Rust

```rust
impl Solution {
pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, 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<Vec<usize>>, values: &Vec<i32>, 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand Down Expand Up @@ -155,7 +155,7 @@ public:
g[a].push_back(b);
g[b].push_back(a);
}
function<long long(int, int)> 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) {
Expand Down Expand Up @@ -231,6 +231,41 @@ function maxKDivisibleComponents(
}
```

#### Rust

```rust
impl Solution {
pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, 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<Vec<usize>>, values: &Vec<i32>, 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Solution {
g[a].push_back(b);
g[b].push_back(a);
}
function<long long(int, int)> 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) {
Expand All @@ -21,4 +21,4 @@ class Solution {
dfs(0, -1);
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
impl Solution {
pub fn max_k_divisible_components(n: i32, edges: Vec<Vec<i32>>, values: Vec<i32>, 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<Vec<usize>>, values: &Vec<i32>, 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
}
}