Skip to content

feat: add rust solutions to lc problems: No.3372,3373 #4445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
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 @@ -323,6 +323,55 @@ function dfs(g: number[][], a: number, fa: number, d: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = edges.len() + 1;
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 as i32);
g[b].push(a as i32);
}
g
}

fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, d: i32) -> i32 {
if d < 0 {
return 0;
}
let mut cnt = 1;
for &b in &g[a] {
if b != fa {
cnt += dfs(g, b as usize, a as i32, d - 1);
}
}
cnt
}

let g2 = build(&edges2);
let m = edges2.len() + 1;
let mut t = 0;
for i in 0..m {
t = t.max(dfs(&g2, i, -1, k - 1));
}

let g1 = build(&edges1);
let n = edges1.len() + 1;
let mut ans = vec![t; n];
for i in 0..n {
ans[i] += dfs(&g1, i, -1, k);
}

ans
}
}
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,55 @@ function dfs(g: number[][], a: number, fa: number, d: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = edges.len() + 1;
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 as i32);
g[b].push(a as i32);
}
g
}

fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, d: i32) -> i32 {
if d < 0 {
return 0;
}
let mut cnt = 1;
for &b in &g[a] {
if b != fa {
cnt += dfs(g, b as usize, a as i32, d - 1);
}
}
cnt
}

let g2 = build(&edges2);
let m = edges2.len() + 1;
let mut t = 0;
for i in 0..m {
t = t.max(dfs(&g2, i, -1, k - 1));
}

let g1 = build(&edges1);
let n = edges1.len() + 1;
let mut ans = vec![t; n];
for i in 0..n {
ans[i] += dfs(&g1, i, -1, k);
}

ans
}
}
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
impl Solution {
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = edges.len() + 1;
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 as i32);
g[b].push(a as i32);
}
g
}

fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, d: i32) -> i32 {
if d < 0 {
return 0;
}
let mut cnt = 1;
for &b in &g[a] {
if b != fa {
cnt += dfs(g, b as usize, a as i32, d - 1);
}
}
cnt
}

let g2 = build(&edges2);
let m = edges2.len() + 1;
let mut t = 0;
for i in 0..m {
t = t.max(dfs(&g2, i, -1, k - 1));
}

let g1 = build(&edges1);
let n = edges1.len() + 1;
let mut ans = vec![t; n];
for i in 0..n {
ans[i] += dfs(&g1, i, -1, k);
}

ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,57 @@ function dfs(g: number[][], a: number, fa: number, c: number[], d: number, cnt:
}
```

#### Rust

```rust
impl Solution {
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = edges.len() + 1;
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 as i32);
g[b].push(a as i32);
}
g
}

fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, c: &mut Vec<i32>, d: i32, cnt: &mut Vec<i32>) {
c[a] = d;
cnt[d as usize] += 1;
for &b in &g[a] {
if b != fa {
dfs(g, b as usize, a as i32, c, d ^ 1, cnt);
}
}
}

let g1 = build(&edges1);
let g2 = build(&edges2);
let n = g1.len();
let m = g2.len();

let mut c1 = vec![0; n];
let mut c2 = vec![0; m];
let mut cnt1 = vec![0; 2];
let mut cnt2 = vec![0; 2];

dfs(&g2, 0, -1, &mut c2, 0, &mut cnt2);
dfs(&g1, 0, -1, &mut c1, 0, &mut cnt1);

let t = cnt2[0].max(cnt2[1]);
let mut ans = vec![0; n];
for i in 0..n {
ans[i] = t + cnt1[c1[i] as usize];
}

ans
}
}
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,57 @@ function dfs(g: number[][], a: number, fa: number, c: number[], d: number, cnt:
}
```

#### Rust

```rust
impl Solution {
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = edges.len() + 1;
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 as i32);
g[b].push(a as i32);
}
g
}

fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, c: &mut Vec<i32>, d: i32, cnt: &mut Vec<i32>) {
c[a] = d;
cnt[d as usize] += 1;
for &b in &g[a] {
if b != fa {
dfs(g, b as usize, a as i32, c, d ^ 1, cnt);
}
}
}

let g1 = build(&edges1);
let g2 = build(&edges2);
let n = g1.len();
let m = g2.len();

let mut c1 = vec![0; n];
let mut c2 = vec![0; m];
let mut cnt1 = vec![0; 2];
let mut cnt2 = vec![0; 2];

dfs(&g2, 0, -1, &mut c2, 0, &mut cnt2);
dfs(&g1, 0, -1, &mut c1, 0, &mut cnt1);

let t = cnt2[0].max(cnt2[1]);
let mut ans = vec![0; n];
for i in 0..n {
ans[i] = t + cnt1[c1[i] as usize];
}

ans
}
}
```

#### C#

```cs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
impl Solution {
pub fn max_target_nodes(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> Vec<i32> {
fn build(edges: &Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = edges.len() + 1;
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 as i32);
g[b].push(a as i32);
}
g
}

fn dfs(g: &Vec<Vec<i32>>, a: usize, fa: i32, c: &mut Vec<i32>, d: i32, cnt: &mut Vec<i32>) {
c[a] = d;
cnt[d as usize] += 1;
for &b in &g[a] {
if b != fa {
dfs(g, b as usize, a as i32, c, d ^ 1, cnt);
}
}
}

let g1 = build(&edges1);
let g2 = build(&edges2);
let n = g1.len();
let m = g2.len();

let mut c1 = vec![0; n];
let mut c2 = vec![0; m];
let mut cnt1 = vec![0; 2];
let mut cnt2 = vec![0; 2];

dfs(&g2, 0, -1, &mut c2, 0, &mut cnt2);
dfs(&g1, 0, -1, &mut c1, 0, &mut cnt1);

let t = cnt2[0].max(cnt2[1]);
let mut ans = vec![0; n];
for i in 0..n {
ans[i] = t + cnt1[c1[i] as usize];
}

ans
}
}