3373. Maximize the Number of Target Nodes After Connecting Trees II
There exist two undirected trees with
n
andm
nodes, labeled from[0, n - 1]
and[0, m - 1]
, respectively.You are given two 2D integer arrays
edges1
andedges2
of lengthsn - 1
andm - 1
, respectively, whereedges1[i] = [ai, bi]
indicates that there is an edge between nodesai
andbi
in the first tree andedges2[i] = [ui, vi]
indicates that there is an edge between nodesui
andvi
in the second tree.Node
u
is target to nodev
if the number of edges on the path fromu
tov
is even. Note that a node is always target to itself.Return an array of
n
integersanswer
, whereanswer[i]
is the maximum possible number of nodes that are target to nodei
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.
1 | class Solution { |