3372. Maximize the Number of Target Nodes After Connecting Trees I
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```dart []
class Solution {
  List<int> maxTargetNodes(List<List<int>> edges1, List<List<int>> edges2, int k) {
    int n = edges1.length + 1;
    int m = edges2.length + 1;
    int maxCount = 0;

    if (k == 0) return List.filled(n, 1);

    List<List<int>> graph = List.generate(m, (_) => []);

    for (var edge in edges2) {
      graph[edge[0]].add(edge[1]);
      graph[edge[1]].add(edge[0]);
    }

    int dfs(int node, int level, int parent, [int count = 1]) {
      if (level < k) {
        for (var child in graph[node]) {
          if (child == parent) continue;
          count += dfs(child, level + 1, node);
        }
      }
      return count;
    }

    for (int node = 0; node < m; node++) {
      maxCount = max(maxCount, dfs(node, 1, -1));
    }

    graph = List.generate(n, (_) => []);
    for (var edge in edges1) {
      graph[edge[0]].add(edge[1]);
      graph[edge[1]].add(edge[0]);
    }

    List<int> result = [];
    for (int node = 0; node < n; node++) {
      result.add(dfs(node, 0, -1) + maxCount);
    }

    return result;
  }
}
```
['Dart']