leetcode-3372-Maximize the Number of Target Nodes After Connecting Trees I

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.

题目

2025-05-28T205059

思路

根据题意,answer数组中每一项都是edges1中对应下标的元素所邻接最近的k个元素个数 与 edges2 中邻接元素个数最多(k - 1)的和

至于这个邻接元素的个数,可以通过dfs遍历每个点所邻接的边来实现。
由于题目限定了是有效的树,那么我们可以认为edges1和edges2不存在环,所以在dfs的时候,只需要保证children != parent即可保证不会重复递归。
由此有有以下代码

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
int n = edges1.length + 1;
List<List<Integer>> record = new ArrayList<>();
for (int x = 0; x < n; x++) {
record.add(new ArrayList<>());
}
int[] sums = new int[n];
for (int[] edge : edges1) {
record.get(edge[0]).add(edge[1]);
record.get(edge[1]).add(edge[0]);
}

for (int x = 0; x < n; x++) {
int d = dfs(x, -1, record, k);
sums[x] = d;
}

// target
int m = edges2.length + 1;
List<List<Integer>> record2 = new ArrayList<>();
for (int x = 0; x < m; x++) {
record2.add(new ArrayList<>());
}
int[] sums2 = new int[m];
for (int[] edge : edges2) {
record2.get(edge[0]).add(edge[1]);
record2.get(edge[1]).add(edge[0]);
}
int max = 0;
for (int x = 0; x < m; x++) {
int d = dfs(x, -1, record2, k - 1);
sums2[x] = d;
max = Math.max(max, sums2[x]);
}
System.out.println(max);
for (int x = 0; x < n; x++) {
sums[x] += max;
}
return sums;

}

int dfs (int n, int p, List<List<Integer>> record, int k) {
if (k < 0) {
return 0;
}
int ans = 1;
List<Integer> children = record.get(n);
for (int c : children) {
if (c == p) {
continue;
}
ans += dfs(c, n, record, k - 1);
}
return ans;
}