Skip to content

feat: add solutions to lc problem: No.3203 #4572

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
Jul 16, 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 @@ -211,7 +211,7 @@ public:
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
d1 := treeDiameter(edges1)
d2 := treeDiameter(edges2)
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
}

func treeDiameter(edges [][]int) (ans int) {
Expand Down Expand Up @@ -275,6 +275,91 @@ function treeDiameter(edges: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
let d1 = Self::tree_diameter(&edges1);
let d2 = Self::tree_diameter(&edges2);
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
}

fn tree_diameter(edges: &Vec<Vec<i32>>) -> 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);
g[b].push(a);
}
let mut ans = 0;
let mut a = 0;
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
for &j in &g[i] {
if j as isize != fa {
dfs(g, j, i as isize, t + 1, ans, a);
}
}
if *ans < t {
*ans = t;
*a = i;
}
}
dfs(&g, 0, -1, 0, &mut ans, &mut a);
dfs(&g, a, -1, 0, &mut ans, &mut a);
ans
}
}
```

#### C#

```cs
public class Solution {
private List<int>[] g;
private int ans;
private int a;

public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
int d1 = TreeDiameter(edges1);
int d2 = TreeDiameter(edges2);
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
}

public int TreeDiameter(int[][] edges) {
int n = edges.Length + 1;
g = new List<int>[n];
for (int k = 0; k < n; ++k) {
g[k] = new List<int>();
}
ans = 0;
a = 0;
foreach (var e in edges) {
int a = e[0], b = e[1];
g[a].Add(b);
g[b].Add(a);
}
Dfs(0, -1, 0);
Dfs(a, -1, 0);
return ans;
}

private void Dfs(int i, int fa, int t) {
foreach (int j in g[i]) {
if (j != fa) {
Dfs(j, i, t + 1);
}
}
if (ans < t) {
ans = t;
a = i;
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public:
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
d1 := treeDiameter(edges1)
d2 := treeDiameter(edges2)
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
}

func treeDiameter(edges [][]int) (ans int) {
Expand Down Expand Up @@ -273,6 +273,91 @@ function treeDiameter(edges: number[][]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
let d1 = Self::tree_diameter(&edges1);
let d2 = Self::tree_diameter(&edges2);
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
}

fn tree_diameter(edges: &Vec<Vec<i32>>) -> 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);
g[b].push(a);
}
let mut ans = 0;
let mut a = 0;
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
for &j in &g[i] {
if j as isize != fa {
dfs(g, j, i as isize, t + 1, ans, a);
}
}
if *ans < t {
*ans = t;
*a = i;
}
}
dfs(&g, 0, -1, 0, &mut ans, &mut a);
dfs(&g, a, -1, 0, &mut ans, &mut a);
ans
}
}
```

#### C#

```cs
public class Solution {
private List<int>[] g;
private int ans;
private int a;

public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
int d1 = TreeDiameter(edges1);
int d2 = TreeDiameter(edges2);
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
}

public int TreeDiameter(int[][] edges) {
int n = edges.Length + 1;
g = new List<int>[n];
for (int k = 0; k < n; ++k) {
g[k] = new List<int>();
}
ans = 0;
a = 0;
foreach (var e in edges) {
int a = e[0], b = e[1];
g[a].Add(b);
g[b].Add(a);
}
Dfs(0, -1, 0);
Dfs(a, -1, 0);
return ans;
}

private void Dfs(int i, int fa, int t) {
foreach (int j in g[i]) {
if (j != fa) {
Dfs(j, i, t + 1);
}
}
if (ans < t) {
ans = t;
a = i;
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class Solution {
private List<int>[] g;
private int ans;
private int a;

public int MinimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
int d1 = TreeDiameter(edges1);
int d2 = TreeDiameter(edges2);
return Math.Max(Math.Max(d1, d2), (d1 + 1) / 2 + (d2 + 1) / 2 + 1);
}

public int TreeDiameter(int[][] edges) {
int n = edges.Length + 1;
g = new List<int>[n];
for (int k = 0; k < n; ++k) {
g[k] = new List<int>();
}
ans = 0;
a = 0;
foreach (var e in edges) {
int a = e[0], b = e[1];
g[a].Add(b);
g[b].Add(a);
}
Dfs(0, -1, 0);
Dfs(a, -1, 0);
return ans;
}

private void Dfs(int i, int fa, int t) {
foreach (int j in g[i]) {
if (j != fa) {
Dfs(j, i, t + 1);
}
}
if (ans < t) {
ans = t;
a = i;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
func minimumDiameterAfterMerge(edges1 [][]int, edges2 [][]int) int {
d1 := treeDiameter(edges1)
d2 := treeDiameter(edges2)
return max(max(d1, d2), (d1+1)/2+(d2+1)/2+1)
return max(d1, d2, (d1+1)/2+(d2+1)/2+1)
}

func treeDiameter(edges [][]int) (ans int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
impl Solution {
pub fn minimum_diameter_after_merge(edges1: Vec<Vec<i32>>, edges2: Vec<Vec<i32>>) -> i32 {
let d1 = Self::tree_diameter(&edges1);
let d2 = Self::tree_diameter(&edges2);
d1.max(d2).max((d1 + 1) / 2 + (d2 + 1) / 2 + 1)
}

fn tree_diameter(edges: &Vec<Vec<i32>>) -> 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);
g[b].push(a);
}
let mut ans = 0;
let mut a = 0;
fn dfs(g: &Vec<Vec<usize>>, i: usize, fa: isize, t: i32, ans: &mut i32, a: &mut usize) {
for &j in &g[i] {
if j as isize != fa {
dfs(g, j, i as isize, t + 1, ans, a);
}
}
if *ans < t {
*ans = t;
*a = i;
}
}
dfs(&g, 0, -1, 0, &mut ans, &mut a);
dfs(&g, a, -1, 0, &mut ans, &mut a);
ans
}
}