Skip to content

Add solution and test-cases for problem 3372 #1217

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
Jun 3, 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
# [3372.Maximize the Number of Target Nodes After Connecting Trees I][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
There exist two **undirected** trees with `n` and `m` nodes, with **distinct** labels in ranges `[0, n - 1]` and `[0, m - 1]`, respectively.

You are given two 2D integer arrays `edges1` and `edges2` of lengths `n - 1` and `m - 1`, respectively, where `edges1[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the first tree and `edges2[i] = [ui, vi]` indicates that there is an edge between nodes `ui` and `vi` in the second tree. You are also given an integer `k`.

Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is less than or equal to `k`. **Note** that a node is always **target** to itself.

Return an array of n integers `answer`, where `answer[i]` is the **maximum** possible number of nodes **target** to node `i` of the first tree if you have to connect one node from the first tree to another node in the second tree.

Note that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.

**Example 1:**

**Example 1:**
![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2

Output: [9,7,9,8,8]

Explanation:

For i = 0, connect node 0 from the first tree to node 0 from the second tree.
For i = 1, connect node 1 from the first tree to node 0 from the second tree.
For i = 2, connect node 2 from the first tree to node 4 from the second tree.
For i = 3, connect node 3 from the first tree to node 4 from the second tree.
For i = 4, connect node 4 from the first tree to node 4 from the second tree.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.png)

### 思路1
> ...
Maximize the Number of Target Nodes After Connecting Trees I
```go
```
Input: edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1

Output: [6,3,3,3,3]

Explanation:

For every i, connect node i of the first tree with any node of the second tree.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package Solution

func Solution(x bool) bool {
return x
import "slices"

func dfs3372(adj map[int][]int, start, parent, limit int) int {
if limit < 0 {
return 0
}

cnt := 1 // self
for _, rel := range adj[start] {
if rel == parent {
continue
}
cnt += dfs3372(adj, rel, start, limit-1)
}
return cnt

}

func calDistance(adj map[int][]int, n, limit int) []int {
distance := make([]int, n)
for i := 0; i < n; i++ {
distance[i] = dfs3372(adj, i, -1, limit)
}
return distance
}

func Solution(edges1 [][]int, edges2 [][]int, k int) []int {
n, m := 0, 0
for _, e := range edges1 {
n = max(n, e[0], e[1])
}
for _, e := range edges2 {
m = max(m, e[0], e[1])
}
n, m = n+1, m+1
adj1 := make(map[int][]int)
adj2 := make(map[int][]int)

for _, e := range edges1 {
adj1[e[0]] = append(adj1[e[0]], e[1])
adj1[e[1]] = append(adj1[e[1]], e[0])
}
for _, e := range edges2 {
adj2[e[0]] = append(adj2[e[0]], e[1])
adj2[e[1]] = append(adj2[e[1]], e[0])
}

dis1 := calDistance(adj1, n, k)
dis2 := calDistance(adj2, m, k-1)

cnt := slices.Max(dis2)
for i := range n {
dis1[i] += cnt
}

return dis1
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
edges1, edges2 [][]int
k int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{0, 1}, {0, 2}, {2, 3}, {2, 4}}, [][]int{{0, 1}, {0, 2}, {0, 3}, {2, 7}, {1, 4}, {1, 5}, {4, 5}, {4, 6}}, 2, []int{9, 7, 9, 8, 8}},
{"TestCase2", [][]int{{0, 1}, {0, 2}, {0, 3}, {0, 4}}, [][]int{{0, 1}, {1, 2}, {2, 3}}, 1, []int{6, 3, 3, 3, 3}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.edges1, c.edges2, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.edges1, c.edges2, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading