3372. Maximize the Number of Target Nodes After Connecting Trees I
Difficulty: Medium
Topics: Tree, Depth-First Search, Breadth-First Search
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 nodeiof the first tree with any node of the second tree.
- For every
Constraints:
2 <= n, m <= 1000edges1.length == n - 1edges2.length == m - 1edges1[i].length == edges2[i].length == 2edges1[i] = [ai, bi]0 <= ai, bi < nedges2[i] = [ui, vi]0 <= ui, vi < m- The input is generated such that
edges1andedges2represent valid trees. 0 <= k <= 1000
Hint:
- For each node
uin the first tree, find the number of nodes at a distance of at mostkfrom nodeu. - For each node
vin the second tree, find the number of nodes at a distance of at mostk - 1from nodev.
Solution:
We need to maximize the number of target nodes for each node in the first tree after connecting it to a node in the second tree. A target node for a given node is any node within a distance of at most k from it. The solution involves efficiently calculating the number of nodes within the specified distance in both trees and then combining these counts optimally.
Approach
Problem Analysis: The problem requires us to compute, for each node in the first tree, the maximum number of target nodes achievable by connecting it to any node in the second tree. The target nodes include nodes from both trees that are within a distance
kfrom the node in the first tree. The connection between the trees allows paths that traverse through the added edge.-
Intuition:
- For any node
iin the first tree, the number of target nodes within the first tree is simply the count of nodes within distancekfromiin the first tree. - When connecting
ito a nodejin the second tree, the number of target nodes in the second tree is the count of nodes within distancek-1fromjin the second tree (since the edgei-jadds 1 to the path length). - To maximize the total target nodes for
i, we should choosejin the second tree that maximizes the count of nodes within distancek-1from it.
- For any node
-
Algorithm Selection:
- Breadth-First Search (BFS): For each node in both trees, perform BFS to count nodes within the specified distance. BFS is suitable because it efficiently explores nodes level by level, allowing us to stop once we exceed the distance limit.
-
Precomputation:
- For each node in the first tree, precompute the count of nodes within distance
k. - For each node in the second tree, precompute the count of nodes within distance
k-1(ifk >= 1), and find the maximum such count (M).
- For each node in the first tree, precompute the count of nodes within distance
-
Result Calculation: For each node in the first tree, the result is the sum of its precomputed count and
M.
-
Complexity Analysis:
-
Tree1 Processing: For each of the
nnodes, BFS runs in O(n) time, leading to O(n²) time. -
Tree2 Processing: Similarly, for each of the
mnodes, BFS runs in O(m) time, leading to O(m²) time. - Overall complexity is O(n² + m²), which is feasible given the constraints (n, m ≤ 1000).
-
Tree1 Processing: For each of the
Let's implement this solution in PHP: 3372. Maximize the Number of Target Nodes After Connecting Trees I
<?php
/**
* @param Integer[][] $edges1
* @param Integer[][] $edges2
* @param Integer $k
* @return Integer[]
*/
function maxTargetNodes($edges1, $edges2, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example 1
$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;
print_r(maxTargetNodes($edges1, $edges2, $k)); // [9,7,9,8,8]
// Example 2
$edges1 = [[0,1],[0,2],[0,3],[0,4]];
$edges2 = [[0,1],[1,2],[2,3]];
$k = 1;
print_r(maxTargetNodes($edges1, $edges2, $k)); // [6,3,3,3,3]
?>
Explanation:
- Graph Construction: The adjacency lists for both trees are built from the given edges.
-
BFS for Tree1: For each node in the first tree, BFS is used to count nodes within distance
k. The count is stored incount1_arr. -
BFS for Tree2: If
k >= 1, for each node in the second tree, BFS counts nodes within distancek-1. The maximum count across all nodes in the second tree is stored inM. -
Result Calculation: For each node in the first tree, the result is the sum of its count from
count1_arrandM, representing the maximum target nodes achievable by connecting it to the optimal node in the second tree.
This approach efficiently computes the solution by leveraging BFS for distance-based counts and combines results from both trees to maximize the target nodes for each query. The complexity is manageable within the problem constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)