Maximize the Number of Target Nodes After Connecting Trees I - Problem

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] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the first tree and edges2[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i 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.

Input & Output

Example 1 — Basic Tree Connection
$ Input: edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1]], k = 3
Output: [6,3,6,3,3]
💡 Note: Tree1 has 5 nodes (0-4), Tree2 has 2 nodes (0-1). With k=3, connecting the trees optimally allows each node to reach different numbers of target nodes. Node 0 and 2 can reach 6 nodes total when connected optimally.
Example 2 — Small Trees
$ Input: edges1 = [[0,1]], edges2 = [[0,1]], k = 1
Output: [3,3]
💡 Note: Each tree has 2 nodes. With k=1, each node in tree1 can reach at most 3 nodes total (itself + 1 neighbor in tree1 + 1 reachable node in tree2 through optimal bridge).
Example 3 — Limited Distance
$ Input: edges1 = [[0,1],[1,2]], edges2 = [[0,1]], k = 1
Output: [3,4,3]
💡 Note: With k=1, middle node 1 can reach more nodes (4 total) since it's centrally located in tree1, while nodes 0 and 2 can reach 3 nodes each.

Constraints

  • 1 ≤ n, m ≤ 1000
  • edges1.length == n - 1
  • edges2.length == m - 1
  • 0 ≤ edges1[i][0], edges1[i][1] < n
  • 0 ≤ edges2[i][0], edges2[i][1] < m
  • 1 ≤ k ≤ 1000

Visualization

Tap to expand
Maximize Target Nodes After Connecting Trees INPUT Tree 1 (n=5 nodes) 0 1 2 3 4 Tree 2 (m=2 nodes) 0 1 edges1=[[0,1],[0,2],[2,3],[2,4]] edges2=[[0,1]] k = 3 (max edges to target) ALGORITHM STEPS 1 Precompute Tree1 Distances BFS from each node to count targets within k edges 2 Precompute Tree2 Distances For each node in Tree2, count reachable within k-1 edges 3 Find Best Tree2 Node Max targets reachable from any node with k-1 budget 4 Combine Results answer[i] = Tree1 targets[i] + max Tree2 contribution Distance Counts (k=3) Tree1: node0=5, node1=2 node2=5, node3=2, node4=2 Tree2(k-1=2): max=1 node Best contribution: +1 FINAL RESULT Optimal Connections 0 1 2 3 4 0 1 connect Output Array: 6 [0] 3 [1] 6 [2] 3 [3] 3 [4] answer = [6, 3, 6, 3, 3] Node 0: 5 in Tree1 + 1 from Tree2 Node 1: 2 in Tree1 + 1 from Tree2 Central nodes reach more! OK - Verified Key Insight: Precompute distances in both trees separately. For Tree1, count nodes reachable within k edges from each node. For Tree2, find the maximum nodes reachable within k-1 edges (1 edge used for connection). The optimal answer for node i = Tree1 reachable count + best Tree2 contribution. Time: O(n^2 + m^2) for BFS precomputation. TutorialsPoint - Maximize the Number of Target Nodes After Connecting Trees I | Optimized - Precompute Distances
Asked in
Google 15 Microsoft 12
8.5K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen