We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Maximize the Number of Target Nodes After Connecting Trees II

Difficulty: Hard


Problem Description

There exist two undirected trees with n and m nodes, labeled from [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] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the first tree and edges2[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i in the second tree. Node u is target to node v if the number of edges on the path from u to v is even. 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 that are target to node i 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. For every query, you will remove the added edge before proceeding to the next query.


Key Insights

  • A node is target to another if the path length between them is even.
  • The parity (odd/even) of the distance from a node to itself and to other nodes can be determined using Depth-First Search (DFS) to mark nodes based on their levels in the tree.
  • The two trees can be treated independently to calculate the number of target nodes based on the connection between a node from the first tree and a node from the second tree.
  • The result for each node in the first tree can be derived from the parity of distances to the nodes in the second tree.

Space and Time Complexity

Time Complexity: O(n + m)
Space Complexity: O(n + m)


Solution

To solve this problem, we can use Depth-First Search (DFS) to classify nodes in both trees based on their parity (even or odd depth). Once we have the counts of even and odd nodes in the second tree, we can calculate the maximum number of target nodes for each node in the first tree. For each node in the first tree, we connect it to a node in the second tree and determine how many nodes are target based on the parity of their depths.


Code Solutions

def maximizeTargetNodes(edges1, edges2):
    from collections import defaultdict
    
    def dfs(tree, node, parent, depth):
        if depth % 2 == 0:
            even_count[0] += 1
        else:
            odd_count[0] += 1
        for neighbor in tree[node]:
            if neighbor != parent:
                dfs(tree, neighbor, node, depth + 1)
    
    # Build the trees
    tree1 = defaultdict(list)
    tree2 = defaultdict(list)
    
    for a, b in edges1:
        tree1[a].append(b)
        tree1[b].append(a)
    
    for u, v in edges2:
        tree2[u].append(v)
        tree2[v].append(u)
    
    # Count even and odd nodes in tree2
    even_count = [0]
    odd_count = [0]
    dfs(tree2, 0, -1, 0)
    
    # Prepare the answer
    answer = []
    for i in range(len(edges1) + 1):
        if i % 2 == 0:
            answer.append(even_count[0])
        else:
            answer.append(odd_count[0])
    
    return answer
← Back to All Questions