There exist two undirected trees with n and m nodes, with distinct labels in ranges [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. You are also given an integer k.
Node u is target to node v if the number of edges on the path from u to v is less than or equal to k. 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 target to node i 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.
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]], k = 2
Output: [9,7,9,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 0 from the second tree.i = 2, connect node 2 from the first tree to node 4 from the second tree.i = 3, connect node 3 from the first tree to node 4 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]], k = 1
Output: [6,3,3,3,3]
Explanation:
For every i, connect node i of the first tree with any node of the second tree.

Constraints:
2 <= n, m <= 1000edges1.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.0 <= k <= 1000class Solution {
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
int n = edges1.length + 1, m = edges2.length + 1;
List<Integer>[] adj1 = new List[n];
List<Integer>[] adj2 = new List[m];
for (int i = 0; i < n; i++)
adj1[i] = new ArrayList<>();
for (int i = 0; i < m; i++)
adj2[i] = new ArrayList<>();
for (int[] e : edges1) {
adj1[e[0]].add(e[1]);
adj1[e[1]].add(e[0]);
}
for (int[] e : edges2) {
adj2[e[0]].add(e[1]);
adj2[e[1]].add(e[0]);
}
int[] ans = new int[n];
// Assuming we connect any node u from tree1 to a node v in tree2, after 1 hop (u->v),
// the rest k-1 steps are identical wrt v (and u will play no role in that). Find v_max_contrib.
// Then ans[u] = nodes within k hops in tree1 from u + the constant v_max_contrib;
int maxContribTree2 = 0;
for (int i = 0; i < m; i++)
maxContribTree2 = Math.max(maxContribTree2, findNeighborsWithinXHops(adj2, i, k));
for (int i = 0; i < n; i++)
ans[i] = maxContribTree2 + findNeighborsWithinXHops(adj1, i, k + 1);
return ans;
}
private int findNeighborsWithinXHops(List<Integer>[] adj2, int src, int hops) {
Queue<Integer> bfs = new LinkedList<>();
bfs.offer(src);
boolean[] vis = new boolean[adj2.length];
int nbrCt = 0;
while (hops-- > 0) {
int size = bfs.size();
nbrCt += size;
while (size-- > 0) {
int top = bfs.poll();
vis[top] = true;
for (int nbr : adj2[top])
if (!vis[nbr])
bfs.add(nbr);
}
}
// System.out.println(src + "\t" + nbrCt);
return nbrCt;
}
}class Solution {
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
int n = edges1.length + 1, m = edges2.length + 1;
List<Integer>[] adj1 = new List[n];
List<Integer>[] adj2 = new List[m];
for (int i = 0; i < n; i++)
adj1[i] = new ArrayList<>();
for (int i = 0; i < m; i++)
adj2[i] = new ArrayList<>();
for (int[] e : edges1) {
adj1[e[0]].add(e[1]);
adj1[e[1]].add(e[0]);
}
for (int[] e : edges2) {
adj2[e[0]].add(e[1]);
adj2[e[1]].add(e[0]);
}
int[] ans = new int[n];
// Assuming we connect any node u from tree1 to a node v in tree2, after 1 hop (u->v),
// the rest k-1 steps are identical wrt v (and u will play no role in that). Find v_max_contrib.
// Then ans[u] = nodes within k hops in tree1 from u + the constant v_max_contrib;
int maxContribTree2 = 0;
for (int i = 0; i < m; i++)
maxContribTree2 = Math.max(maxContribTree2, findNeighborsWithinKHops(adj2, i, k - 1, -1));
for (int i = 0; i < n; i++)
ans[i] = maxContribTree2 + findNeighborsWithinKHops(adj1, i, k, -1);
return ans;
}
private int findNeighborsWithinKHops(List<Integer>[] adj2, int src, int k, int parent) {
if (k < 0)
return 0;
int nbrCt = 1;
for (int nbr : adj2[src])
if (nbr != parent)
nbrCt += findNeighborsWithinKHops(adj2, nbr, k - 1, src);
return nbrCt;
}
}class Solution {
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
int n = edges1.length + 1, m = edges2.length + 1;
List<Integer>[] adj1 = new List[n];
List<Integer>[] adj2 = new List[m];
for (int i = 0; i < n; i++)
adj1[i] = new ArrayList<>();
for (int i = 0; i < m; i++)
adj2[i] = new ArrayList<>();
for (int[] e : edges1) {
adj1[e[0]].add(e[1]);
adj1[e[1]].add(e[0]);
}
for (int[] e : edges2) {
adj2[e[0]].add(e[1]);
adj2[e[1]].add(e[0]);
}
int[] ans = new int[n];
// Assuming we connect any node u from tree1 to a node v in tree2, after 1 hop (u->v),
// the rest k-1 steps are identical wrt v (and u will play no role in that). Find v_max_contrib.
// Then ans[u] = nodes within k hops in tree1 from u + the constant v_max_contrib;
int maxContribTree2 = 0;
for (int i = 0; i < m; i++)
maxContribTree2 = Math.max(maxContribTree2, findNeighborsWithinXHops(adj2, i, k));
for (int i = 0; i < n; i++)
ans[i] = maxContribTree2 + findNeighborsWithinXHops(adj1, i, k + 1);
return ans;
}
private int findNeighborsWithinXHops(List<Integer>[] adj2, int src, int hops) {
Queue<Integer> bfs = new LinkedList<>();
bfs.offer(src);
boolean[] vis = new boolean[adj2.length];
int nbrCt = 0;
while (hops-- > 0) {
int size = bfs.size();
nbrCt += size;
while (size-- > 0) {
int top = bfs.poll();
vis[top] = true;
for (int nbr : adj2[top])
if (!vis[nbr])
bfs.add(nbr);
}
}
// System.out.println(src + "\t" + nbrCt);
return nbrCt;
}
}