Maximize the Number of Target Nodes After Connecting Trees I - Problem
Tree Connection Optimization Challenge

You're given two separate undirected trees and need to strategically connect them to maximize reachability. The first tree has n nodes labeled [0, n-1] and the second tree has m nodes labeled [0, m-1].

Key Concepts:
• A node u is target to node v if the shortest path between them has ≤ k edges
• Each node is always target to itself
• You can connect any node from tree 1 to any node from tree 2 with a single edge

Goal: For each node i in the first tree, find the maximum number of nodes that can be target to i after optimally connecting the trees.

Input:
edges1: Array of edges for the first tree
edges2: Array of edges for the second tree
k: Maximum allowed distance for target relationship

Output: Array where answer[i] = maximum target nodes for node i

Input & Output

example_1.py — Basic Tree Connection
$ Input: edges1 = [[0,1],[0,2],[1,3],[1,4]], edges2 = [[0,1],[0,2],[0,3]], k = 2
Output: [3, 6, 3, 5, 5]
💡 Note: For node 0 in tree1: it can reach nodes {0,1,2} in tree1 (distance ≤ 2). With optimal bridge connection, no additional nodes from tree2 are reachable within remaining distance, so answer is 3.
example_2.py — Larger Distance Budget
$ Input: edges1 = [[0,1],[0,2]], edges2 = [[0,1],[1,2]], k = 3
Output: [6, 5, 5]
💡 Note: With k=3, nodes can reach further. Node 0 can reach all nodes in tree1, and with optimal bridge, can also reach all nodes in tree2, giving total of 6 nodes.
example_3.py — Small Distance Budget
$ Input: edges1 = [[0,1],[1,2]], edges2 = [[0,1]], k = 1
Output: [2, 3, 2]
💡 Note: With k=1, each node can only reach immediate neighbors. Node 1 has the most connections and benefits most from optimal bridge placement.

Constraints

  • 2 ≤ n, m ≤ 1000
  • edges1.length == n - 1
  • edges2.length == m - 1
  • edges1[i].length == edges2[i].length == 2
  • 0 ≤ ai, bi < n
  • 0 ≤ ui, vi < m
  • 1 ≤ k ≤ n + m
  • The input represents valid trees

Visualization

Tap to expand
Neighborhood 1QueryNeighborhood 2Optimal BridgeBridge Optimization Process1. Calculate distances within each neighborhood2. For query resident, count reachable locations in home neighborhood3. Test each possible bridge position and calculate additional reach4. Select bridge that maximizes total accessible locations
Understanding the Visualization
1
Survey Neighborhoods
Map all distances within each neighborhood (tree)
2
Calculate Base Reach
For each resident, count reachable locations in home neighborhood
3
Test Bridge Positions
For each possible bridge, calculate additional reachable locations
4
Select Optimal Bridge
Choose bridge position that maximizes total accessibility
Key Takeaway
🎯 Key Insight: Pre-computing all distances allows us to efficiently test bridge positions without recalculating paths, leading to significant performance improvement over brute force approaches.
Asked in
Google 28 Meta 22 Amazon 18 Microsoft 15
34.7K Views
Medium Frequency
~25 min Avg. Time
1.2K 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