Tap here to switch tabs
Problem
Submissions

Problem: Maximize Target Nodes After Connecting Trees I

Try to solve the Maximize the Number of Target Nodes After Connecting Trees I problem.

Statement

You are given two undirected trees with nn and mm nodes, labeled from 00 to n−1n - 1 and 00 to m−1m - 1, respectively.

You are given two 22D integer arrays edges1 of length n−1n - 1 and edges2 of length m−1m - 1, where edges1[i] = [a_i, b_i] denotes an edge between nodes a_i and b_i in the first tree, and edges2[i] = [u_i, v_i] denotes an edge between nodes u_i and v_i in the second tree. You are also given an integer k.

A node uu is considered a target to node vv if the distance between them is at most k.

For each node ii from 00 to n−1n - 1, you must add exactly one undirected edge connecting node ii in the first tree to any node in the second tree.

Return an array answer of size nn, where answer[i] represents the maximum possible number of target nodes from node ii after adding the optimal edge between node ii and the second tree.

Note: The distance between two nodes is defined as the number of edges on the path connecting them. A node is always a target to itself since the distance is 00.

Constraints:

  • 2≤n,m≤10002 \leq n, m \leq 1000

  • edges1.length ==n−1== n - 1

  • edges2.length ==m−1== m - 1

  • edges1[i].length ==== edges2[i].length ==2== 2

  • edges1[i] =[ai,bi]= [a_i, b_i]

  • 0≤ai,bi<n0 \leq a_i, b_i < n

  • edges2[i] =[ui,vi]= [u_i, v_i]

  • 0≤ui,vi<m0 \leq u_i, v_i < m

  • The input is generated such that edges1 and edges2 represent valid trees.

  • 0≤k≤10000 \leq k \leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximize Target Nodes After Connecting Trees I

Try to solve the Maximize the Number of Target Nodes After Connecting Trees I problem.

Statement

You are given two undirected trees with nn and mm nodes, labeled from 00 to n−1n - 1 and 00 to m−1m - 1, respectively.

You are given two 22D integer arrays edges1 of length n−1n - 1 and edges2 of length m−1m - 1, where edges1[i] = [a_i, b_i] denotes an edge between nodes a_i and b_i in the first tree, and edges2[i] = [u_i, v_i] denotes an edge between nodes u_i and v_i in the second tree. You are also given an integer k.

A node uu is considered a target to node vv if the distance between them is at most k.

For each node ii from 00 to n−1n - 1, you must add exactly one undirected edge connecting node ii in the first tree to any node in the second tree.

Return an array answer of size nn, where answer[i] represents the maximum possible number of target nodes from node ii after adding the optimal edge between node ii and the second tree.

Note: The distance between two nodes is defined as the number of edges on the path connecting them. A node is always a target to itself since the distance is 00.

Constraints:

  • 2≤n,m≤10002 \leq n, m \leq 1000

  • edges1.length ==n−1== n - 1

  • edges2.length ==m−1== m - 1

  • edges1[i].length ==== edges2[i].length ==2== 2

  • edges1[i] =[ai,bi]= [a_i, b_i]

  • 0≤ai,bi<n0 \leq a_i, b_i < n

  • edges2[i] =[ui,vi]= [u_i, v_i]

  • 0≤ui,vi<m0 \leq u_i, v_i < m

  • The input is generated such that edges1 and edges2 represent valid trees.

  • 0≤k≤10000 \leq k \leq 1000