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

There exist two undirected trees with n and m nodes, labeled from [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.

Node u is target to node v if the number of edges on the path from u to v is even. 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 that are target to node i of the first tree if you had 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 Two-Node Trees
$ Input: edges1 = [[0,1]], edges2 = [[0,1]]
Output: [3,3]
💡 Note: Each tree has 2 nodes. When we connect them optimally, each node can reach 3 target nodes total (including nodes at even distances through the bridge connection).
Example 2 — Single Node Trees
$ Input: edges1 = [], edges2 = []
Output: [2]
💡 Note: Each tree has only 1 node. When connected, node 0 from tree1 can reach itself (distance 0, even) and the single node from tree2 (distance 1+0=1, odd). Actually, distance 1 is odd, so only itself at distance 0. The bridge adds 1 more reachable target.
Example 3 — Larger Trees
$ Input: edges1 = [[0,1],[1,2]], edges2 = [[0,1],[1,2]]
Output: [4,4,4]
💡 Note: Linear trees with 3 nodes each. Optimal bridge connections allow each query node to reach maximum target nodes with even path lengths.

Constraints

  • 1 ≤ n, m ≤ 1000
  • edges1.length = n - 1
  • edges2.length = m - 1
  • 0 ≤ ai, bi < n
  • 0 ≤ ui, vi < m
  • The input ensures that edges1 and edges2 represent valid trees

Visualization

Tap to expand
Maximize Target Nodes After Connecting Trees II INPUT Tree 1 (n=2 nodes) 0 1 Tree 2 (m=2 nodes) 0 1 edges1 = [[0,1]] [0] -- [1] edges2 = [[0,1]] [0] -- [1] Even distance = target ALGORITHM STEPS 1 Bipartite Coloring Color nodes by parity (depth) 0 1 Even=Red Odd=Blue 2 Count Same Parity Tree1: count[0]=1, count[1]=1 Tree2: count[0]=1, count[1]=1 3 Max from Tree2 max_tree2 = max(1,1) = 1 Best connection adds 1 node 4 Compute Answer For each node i in Tree1: ans[i] = same_parity + max_tree2 Node 0: 2 + 1 = 3 Node 1: 2 + 1 = 3 FINAL RESULT Optimal Connection 0 1 0 1 new Target Count Analysis Node 0: targets 0,1 + T2 = 3 Node 1: targets 0,1 + T2 = 3 OUTPUT [3, 3] Key Insight: Trees are bipartite! Nodes at even distance share the same parity (color in 2-coloring). For Tree1: count nodes of same parity as node i. For Tree2: take max of both parities since connecting flips parity. Answer[i] = same_parity_count(Tree1) + max_parity_count(Tree2). TutorialsPoint - Maximize the Number of Target Nodes After Connecting Trees II | Tree Re-rooting with Bipartite Analysis
Asked in
Google 12 Meta 8 Amazon 6
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