3372. Maximize the Number of Target Nodes After Connecting Trees I
There exist two undirected trees with
n
andm
nodes, with distinct labels in ranges[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. You are also given an integerk
.Node
u
is target to nodev
if the number of edges on the path fromu
tov
is less than or equal tok
. Note that a node is always target to itself.Return an array of
n
integersanswer
, whereanswer[i]
is the maximum possible number of nodes target to nodei
of the first tree if you have 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 { |