Skip to content

Commit 7c2ed94

Browse files
authored
feat: add rust solutions to lc problems: No.3372,3373 (#4445)
* No.3372.Maximize the Number of Target Nodes After Connecting Trees I * No.3373.Maximize the Number of Target Nodes After Connecting Trees II
1 parent 30d02ab commit 7c2ed94

File tree

6 files changed

+290
-0
lines changed

6 files changed

+290
-0
lines changed

solution/3300-3399/3372.Maximize the Number of Target Nodes After Connecting Trees I/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,55 @@ function dfs(g: number[][], a: number, fa: number, d: number): number {
323323
}
324324
```
325325

326+
#### Rust
327+
328+
```rust
329+
impl Solution {
330+
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
331+
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
332+
let n = edges.len() + 1;
333+
let mut g = vec![vec![]; n];
334+
for e in edges {
335+
let a = e[0] as usize;
336+
let b = e[1] as usize;
337+
g[a].push(b as i32);
338+
g[b].push(a as i32);
339+
}
340+
g
341+
}
342+
343+
fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, d: i32) -> i32 {
344+
if d < 0 {
345+
return 0;
346+
}
347+
let mut cnt = 1;
348+
for &b in &g[a] {
349+
if b != fa {
350+
cnt += dfs(g, b as usize, a as i32, d - 1);
351+
}
352+
}
353+
cnt
354+
}
355+
356+
let g2 = build(&edges2);
357+
let m = edges2.len() + 1;
358+
let mut t = 0;
359+
for i in 0..m {
360+
t = t.max(dfs(&g2, i, -1, k - 1));
361+
}
362+
363+
let g1 = build(&edges1);
364+
let n = edges1.len() + 1;
365+
let mut ans = vec![t; n];
366+
for i in 0..n {
367+
ans[i] += dfs(&g1, i, -1, k);
368+
}
369+
370+
ans
371+
}
372+
}
373+
```
374+
326375
#### C#
327376

328377
```cs

solution/3300-3399/3372.Maximize the Number of Target Nodes After Connecting Trees I/README_EN.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,55 @@ function dfs(g: number[][], a: number, fa: number, d: number): number {
318318
}
319319
```
320320

321+
#### Rust
322+
323+
```rust
324+
impl Solution {
325+
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
326+
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
327+
let n = edges.len() + 1;
328+
let mut g = vec![vec![]; n];
329+
for e in edges {
330+
let a = e[0] as usize;
331+
let b = e[1] as usize;
332+
g[a].push(b as i32);
333+
g[b].push(a as i32);
334+
}
335+
g
336+
}
337+
338+
fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, d: i32) -> i32 {
339+
if d < 0 {
340+
return 0;
341+
}
342+
let mut cnt = 1;
343+
for &b in &g[a] {
344+
if b != fa {
345+
cnt += dfs(g, b as usize, a as i32, d - 1);
346+
}
347+
}
348+
cnt
349+
}
350+
351+
let g2 = build(&edges2);
352+
let m = edges2.len() + 1;
353+
let mut t = 0;
354+
for i in 0..m {
355+
t = t.max(dfs(&g2, i, -1, k - 1));
356+
}
357+
358+
let g1 = build(&edges1);
359+
let n = edges1.len() + 1;
360+
let mut ans = vec![t; n];
361+
for i in 0..n {
362+
ans[i] += dfs(&g1, i, -1, k);
363+
}
364+
365+
ans
366+
}
367+
}
368+
```
369+
321370
#### C#
322371

323372
```cs
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
impl Solution {
2+
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
3+
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
4+
let n = edges.len() + 1;
5+
let mut g = vec![vec![]; n];
6+
for e in edges {
7+
let a = e[0] as usize;
8+
let b = e[1] as usize;
9+
g[a].push(b as i32);
10+
g[b].push(a as i32);
11+
}
12+
g
13+
}
14+
15+
fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, d: i32) -> i32 {
16+
if d < 0 {
17+
return 0;
18+
}
19+
let mut cnt = 1;
20+
for &b in &g[a] {
21+
if b != fa {
22+
cnt += dfs(g, b as usize, a as i32, d - 1);
23+
}
24+
}
25+
cnt
26+
}
27+
28+
let g2 = build(&edges2);
29+
let m = edges2.len() + 1;
30+
let mut t = 0;
31+
for i in 0..m {
32+
t = t.max(dfs(&g2, i, -1, k - 1));
33+
}
34+
35+
let g1 = build(&edges1);
36+
let n = edges1.len() + 1;
37+
let mut ans = vec![t; n];
38+
for i in 0..n {
39+
ans[i] += dfs(&g1, i, -1, k);
40+
}
41+
42+
ans
43+
}
44+
}

solution/3300-3399/3373.Maximize the Number of Target Nodes After Connecting Trees II/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,57 @@ function dfs(g: number[][], a: number, fa: number, c: number[], d: number, cnt:
319319
}
320320
```
321321

322+
#### Rust
323+
324+
```rust
325+
impl Solution {
326+
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {
327+
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
328+
let n = edges.len() + 1;
329+
let mut g = vec![vec![]; n];
330+
for e in edges {
331+
let a = e[0] as usize;
332+
let b = e[1] as usize;
333+
g[a].push(b as i32);
334+
g[b].push(a as i32);
335+
}
336+
g
337+
}
338+
339+
fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, c: &mut Vec<i32>, d: i32, cnt: &mut Vec<i32>) {
340+
c[a] = d;
341+
cnt[d as usize] += 1;
342+
for &b in &g[a] {
343+
if b != fa {
344+
dfs(g, b as usize, a as i32, c, d ^ 1, cnt);
345+
}
346+
}
347+
}
348+
349+
let g1 = build(&edges1);
350+
let g2 = build(&edges2);
351+
let n = g1.len();
352+
let m = g2.len();
353+
354+
let mut c1 = vec![0; n];
355+
let mut c2 = vec![0; m];
356+
let mut cnt1 = vec![0; 2];
357+
let mut cnt2 = vec![0; 2];
358+
359+
dfs(&g2, 0, -1, &mut c2, 0, &mut cnt2);
360+
dfs(&g1, 0, -1, &mut c1, 0, &mut cnt1);
361+
362+
let t = cnt2[0].max(cnt2[1]);
363+
let mut ans = vec![0; n];
364+
for i in 0..n {
365+
ans[i] = t + cnt1[c1[i] as usize];
366+
}
367+
368+
ans
369+
}
370+
}
371+
```
372+
322373
#### C#
323374

324375
```cs

solution/3300-3399/3373.Maximize the Number of Target Nodes After Connecting Trees II/README_EN.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,57 @@ function dfs(g: number[][], a: number, fa: number, c: number[], d: number, cnt:
314314
}
315315
```
316316

317+
#### Rust
318+
319+
```rust
320+
impl Solution {
321+
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {
322+
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
323+
let n = edges.len() + 1;
324+
let mut g = vec![vec![]; n];
325+
for e in edges {
326+
let a = e[0] as usize;
327+
let b = e[1] as usize;
328+
g[a].push(b as i32);
329+
g[b].push(a as i32);
330+
}
331+
g
332+
}
333+
334+
fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, c: &mut Vec<i32>, d: i32, cnt: &mut Vec<i32>) {
335+
c[a] = d;
336+
cnt[d as usize] += 1;
337+
for &b in &g[a] {
338+
if b != fa {
339+
dfs(g, b as usize, a as i32, c, d ^ 1, cnt);
340+
}
341+
}
342+
}
343+
344+
let g1 = build(&edges1);
345+
let g2 = build(&edges2);
346+
let n = g1.len();
347+
let m = g2.len();
348+
349+
let mut c1 = vec![0; n];
350+
let mut c2 = vec![0; m];
351+
let mut cnt1 = vec![0; 2];
352+
let mut cnt2 = vec![0; 2];
353+
354+
dfs(&g2, 0, -1, &mut c2, 0, &mut cnt2);
355+
dfs(&g1, 0, -1, &mut c1, 0, &mut cnt1);
356+
357+
let t = cnt2[0].max(cnt2[1]);
358+
let mut ans = vec![0; n];
359+
for i in 0..n {
360+
ans[i] = t + cnt1[c1[i] as usize];
361+
}
362+
363+
ans
364+
}
365+
}
366+
```
367+
317368
#### C#
318369

319370
```cs
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
impl Solution {
2+
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {
3+
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
4+
let n = edges.len() + 1;
5+
let mut g = vec![vec![]; n];
6+
for e in edges {
7+
let a = e[0] as usize;
8+
let b = e[1] as usize;
9+
g[a].push(b as i32);
10+
g[b].push(a as i32);
11+
}
12+
g
13+
}
14+
15+
fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, c: &mut Vec<i32>, d: i32, cnt: &mut Vec<i32>) {
16+
c[a] = d;
17+
cnt[d as usize] += 1;
18+
for &b in &g[a] {
19+
if b != fa {
20+
dfs(g, b as usize, a as i32, c, d ^ 1, cnt);
21+
}
22+
}
23+
}
24+
25+
let g1 = build(&edges1);
26+
let g2 = build(&edges2);
27+
let n = g1.len();
28+
let m = g2.len();
29+
30+
let mut c1 = vec![0; n];
31+
let mut c2 = vec![0; m];
32+
let mut cnt1 = vec![0; 2];
33+
let mut cnt2 = vec![0; 2];
34+
35+
dfs(&g2, 0, -1, &mut c2, 0, &mut cnt2);
36+
dfs(&g1, 0, -1, &mut c1, 0, &mut cnt1);
37+
38+
let t = cnt2[0].max(cnt2[1]);
39+
let mut ans = vec![0; n];
40+
for i in 0..n {
41+
ans[i] = t + cnt1[c1[i] as usize];
42+
}
43+
44+
ans
45+
}
46+
}

0 commit comments

Comments
 (0)