Skip to content

3373. Maximize the Number of Target Nodes After Connecting Trees II

  • For i = 2, connect node 2 from the first tree to node 7 from the second tree.
  • For i = 3, connect node 3 from the first tree to node 0 from the second tree.
  • For i = 4, connect node 4 from the first tree to node 4 from the second tree.

img

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.

img

Constraints:

  • 2 <= n, m <= 105
  • edges1.length == n - 1
  • edges2.length == m - 1
  • edges1[i].length == edges2[i].length == 2
  • edges1[i] = [ai, bi]
  • 0 <= ai, bi < n
  • edges2[i] = [ui, vi]
  • 0 <= ui, vi < m
  • The input is generated such that edges1 and edges2 represent valid trees.

Solution:

```java