3372. Maximize the Number of Target Nodes After Connecting Trees I
There exist two undirected trees with
nandmnodes, with distinct labels in ranges[0, n - 1]and[0, m - 1], respectively.You are given two 2D integer arrays
edges1andedges2of lengthsn - 1andm - 1, respectively, whereedges1[i] = [ai, bi]indicates that there is an edge between nodesaiandbiin the first tree andedges2[i] = [ui, vi]indicates that there is an edge between nodesuiandviin the second tree. You are also given an integerk.Node
uis target to nodevif the number of edges on the path fromutovis less than or equal tok. Note that a node is always target to itself.Return an array of
nintegersanswer, whereanswer[i]is the maximum possible number of nodes target to nodeiof 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 { |