From 3b8f64182eed3df203d7062d3148691968c6c440 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 17 Jul 2025 06:51:36 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3203 No.3203.Find Minimum Diameter After Merging Two Trees --- .../README.md | 87 ++++++++++++++++++- .../README_EN.md | 87 ++++++++++++++++++- .../Solution.cs | 41 +++++++++ .../Solution.go | 2 +- .../Solution.rs | 34 ++++++++ 5 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.cs create mode 100644 solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.rs diff --git a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README.md b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README.md index 58092b08bfd82..f4e7b33ad23ad 100644 --- a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README.md +++ b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README.md @@ -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) { @@ -275,6 +275,91 @@ function treeDiameter(edges: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_diameter_after_merge(edges1: Vec>, edges2: Vec>) -> 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>) -> 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>, 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[] 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[n]; + for (int k = 0; k < n; ++k) { + g[k] = new List(); + } + 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; + } + } +} +``` + diff --git a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README_EN.md b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README_EN.md index ef28a10e96a3f..af113252eeb44 100644 --- a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README_EN.md +++ b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/README_EN.md @@ -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) { @@ -273,6 +273,91 @@ function treeDiameter(edges: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_diameter_after_merge(edges1: Vec>, edges2: Vec>) -> 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>) -> 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>, 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[] 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[n]; + for (int k = 0; k < n; ++k) { + g[k] = new List(); + } + 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; + } + } +} +``` + diff --git a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.cs b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.cs new file mode 100644 index 0000000000000..39db451c64acf --- /dev/null +++ b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.cs @@ -0,0 +1,41 @@ +public class Solution { + private List[] 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[n]; + for (int k = 0; k < n; ++k) { + g[k] = new List(); + } + 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; + } + } +} diff --git a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.go b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.go index 4ab6408b235ad..2a69958fe6b8c 100644 --- a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.go +++ b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.go @@ -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) { diff --git a/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.rs b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.rs new file mode 100644 index 0000000000000..cca07c6786955 --- /dev/null +++ b/solution/3200-3299/3203.Find Minimum Diameter After Merging Two Trees/Solution.rs @@ -0,0 +1,34 @@ +impl Solution { + pub fn minimum_diameter_after_merge(edges1: Vec>, edges2: Vec>) -> 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>) -> 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>, 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 + } +}