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.
Example 1:
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:
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.
Constraints:
2 <= n, m <= 1000
edges1.length == n - 1
edges2.length == m - 1
edges1[i].length == edges2[i].length == 2
edges1[i] = [ai, bi]
0 <= ai, bi < n
edges2[i] = [ui, vi]
0 <= ui, vi < m
- The input is generated such that
edges1
andedges2
represent valid trees. 0 <= k <= 1000
Solution:
看不懂题
Tree 1 for node [i] connect to Tree 2,
for example, node 1 from tree 1 it can connect to the tree2 Node (0, 1, 2, 3, 4, 5, 6, 7). And then find the situtation which only <= k edge can pick most node. Example show in the following.
Node 0 - Node 0 = 9
Node 0 - Node 1 = 8
Node 0 - Node 2 = 8
Node 0 - Node 3 = 7
Node 0 - Node 4 = 9
Node 0 - Node 5 = 7
Node 0 - Node 6 = 7
Node 0 - Node7 = 7
Math.max for Tree 1 Node 1 = 9
...
[9, 7, 9, 8, 8]
Thinking:
Tree2 find the max which node can has <=(k-1) edge node
BFS
Solution:
import java.util.*;
class Solution {
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
// 如果 k 为 0,直接返回第一棵树中每个节点的目标节点数
if (k == 0) {
return calculateTreeTargetsBFS(edges1, k);
}
// 计算第二棵树中最大目标节点数
Map<Integer, List<Integer>> tree2 = buildTree(edges2);
int maxTargetsInTree2 = 0;
for (int node : tree2.keySet()) {
maxTargetsInTree2 = Math.max(maxTargetsInTree2, bfs(node, tree2, k - 1));
}
// 计算第一棵树中每个节点的目标节点数,并加上第二棵树的最大贡献
Map<Integer, List<Integer>> tree1 = buildTree(edges1);
int[] result = new int[edges1.length + 1];
for (int i = 0; i < result.length; i++) {
result[i] = bfs(i, tree1, k) + maxTargetsInTree2;
}
return result;
}
private Map<Integer, List<Integer>> buildTree(int[][] edges) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] edge : edges) {
int u = edge[0], v = edge[1];
graph.putIfAbsent(u, new ArrayList<>());
graph.putIfAbsent(v, new ArrayList<>());
graph.get(u).add(v);
graph.get(v).add(u);
}
return graph;
}
private int bfs(int start, Map<Integer, List<Integer>> graph, int k) {
Queue<int[]> queue = new LinkedList<>();
Set<Integer> visited = new HashSet<>();
queue.offer(new int[]{start, 0}); // {当前节点, 当前深度}
visited.add(start);
int count = 0;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int node = current[0];
int depth = current[1];
if (depth > k) {
continue; // 超过深度 k,停止扩展
}
count++;
for (int neighbor : graph.getOrDefault(node, new ArrayList<>())) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.offer(new int[]{neighbor, depth + 1});
}
}
}
return count;
}
private int[] calculateTreeTargetsBFS(int[][] edges, int k) {
Map<Integer, List<Integer>> tree = buildTree(edges);
int[] targets = new int[edges.length + 1];
for (int i = 0; i < targets.length; i++) {
targets[i] = bfs(i, tree, k);
}
return targets;
}
}
https://leetcode.cn/problems/maximize-the-number-of-target-nodes-after-connecting-trees-i/solutions/3006334/nao-jin-ji-zhuan-wan-bao-li-mei-ju-pytho-ua6k/