Problem solution · C++

Maximize the Number of Target Nodes After Connecting Trees II

Maximize the Number of Target Nodes After Connecting Trees II: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Depth-first search
Source
walkccc LeetCode Solutions
Length
51 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Maximize the Number of Target Nodes After Connecting Trees II, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 51 lines of C++ from the credited upstream file 3373.cpp.
  • The implementation visibly relies on sequence storage.
  • 3 loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximize the Number of Target Nodes After Connecting Trees II · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  vector<int> maxTargetNodes(vector<vector<int>>& edges1,                             vector<vector<int>>& edges2) {    const int n = edges1.size() + 1;    const int m = edges2.size() + 1;    vector<int> ans;    const vector<vector<int>> graph1 = buildGraph(edges1);    const vector<vector<int>> graph2 = buildGraph(edges2);    vector<bool> parity1(n);    vector<bool> parity2(m);  // placeholder (parity2 is not used)    const int even1 = dfs(graph1, 0, -1, parity1, /*isEven=*/true);    const int even2 = dfs(graph2, 0, -1, parity2, /*isEven=*/true);    const int odd1 = n - even1;    const int odd2 = m - even2;     for (int i = 0; i < n; ++i) {      const int tree1 = parity1[i] ? even1 : odd1;      // Can connect the current node in tree1 to either an even node or an odd      // node in tree2.      const int tree2 = max(even2, odd2);      ans.push_back(tree1 + tree2);    }     return ans;  }  private:  // Returns the number of nodes that can be reached from u with even steps.  int dfs(const vector<vector<int>>& graph, int u, int prev,          vector<bool>& parity, bool isEven) {    int res = isEven ? 1 : 0;    parity[u] = isEven;    for (const int v : graph[u])      if (v != prev)        res += dfs(graph, v, u, parity, !isEven);    return res;  }   vector<vector<int>> buildGraph(const vector<vector<int>>& edges) {    vector<vector<int>> graph(edges.size() + 1);    for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      graph[u].push_back(v);      graph[v].push_back(u);    }    return graph;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗