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.
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]]
Output: [8,7,7,8,8]
Explanation:
i = 0, connect node 0 from the first tree to node 0 from the second tree.i = 1, connect node 1 from the first tree to node 4 from the second tree.i = 2, connect node 2 from the first tree to node 7 from the second tree.i = 3, connect node 3 from the first tree to node 0 from the second tree.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]]
Output: [3,6,6,6,6]
Explanation:
For every i, connect node i of the first tree with any node of the second tree.

Constraints:
2 <= n, m <= 105edges1.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 < medges1 and edges2 represent valid trees.To determine the maximum number of nodes in the second tree that are 'target' to a specific node in the first tree, explore the parity of paths in each tree independently using Depth First Search (DFS). For each source node in the first tree, connect it with each node in the second tree to compute the union of target nodes. This requires understanding how the parity of the path from the source node to another node affects its 'target' status.
This solution involves setting up a DFS method to compute path parities in both trees independently. A main routine calls DFS from each node in the first tree, temporarily adds and evaluates connections to nodes in the second tree, allowing us to compare results for each base node efficiently.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * m) due to checking each second tree node for each first tree node.
Space Complexity: O(n + m) for storing parity and connectivity information in auxiliary arrays.
Alternatively, use dynamic programming to track path parities across disjoint subproblems. Here, linked lists or adjacency tables establish node connections. By leveraging dynamic techniques, each node's potential 'target' connections are recursively built and stored, facilitating rapid evaluations.
This solution focuses on memoizing parity-based results for individual queries. By storing intermediate connectivity results, expensive recalculations are avoided.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * m) but with reduced recalculation overhead.
Space Complexity: O(n + m) due to memoization arrays and list structures.
| Approach | Complexity |
|---|---|
| DFS with Pre-computed Parities | Time Complexity: O(n * m) due to checking each second tree node for each first tree node. |
| Dynamic Programming with Path Parity Tracking | Time Complexity: O(n * m) but with reduced recalculation overhead. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,307 views views
Watch 9 more video solutions →Practice Maximize the Number of Target Nodes After Connecting Trees II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor