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
Connect Two Trees to Maximize Target NodesTree 1 (n=5 nodes)01234Tree 2 (m=2 nodes)01Bridge ConnectionTry all possible connectionsk = 3 (max distance)Find bridge that maximizesreachable nodes for eachstarting node in Tree 1Output: [6,3,6,3,3] - max targets per node
Understanding the Visualization
1
Input
Two separate trees with edges and distance limit k
2
Connect
Add one bridge edge between any two nodes from different trees
3
Output
Maximum target nodes reachable within k steps for each node in tree1
Key Takeaway
🎯 Key Insight: Precompute reachable counts in each tree separately, then find optimal bridge connections
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