Simple DFS
O(n + m)\n\n# Code\npython3 []\nclass Solution:\n    def maxTargetNodes(self, edges1: List[List[int]], edges2: List[List[int]], k: int) -> List[int]:\n        \
O(n + m)\n\n# Code\n```python3 []\nclass Solution:\n    def maxTargetNodes(self, edges1: List[List[int]], edges2: List[List[int]], k: int) -> List[int]:\n        \n        graph2 = defaultdict(list)\n        for start, end in edges2:\n            graph2[start].append(end)\n            graph2[end].append(start)\n\n        def dfs(node, steps):\n            if steps == k or node in self.seen:\n                return\n            \n            self.cnt += 1\n            self.seen.add(node)\n            for child in self.graph[node]:\n                dfs(child, steps + 1)\n        \n        lookup2 = defaultdict(int)\n        self.graph = deepcopy(graph2)\n\n        # we look uptill depth k - 1 for graph2\n        for i in range(0, len(edges2) + 1):\n            self.cnt = 0\n            self.seen = set()\n            dfs(i, 0)\n            lookup2[i] = self.cnt\n\n        graph1 = defaultdict(list)\n        for start, end in edges1:\n            graph1[start].append(end)\n            graph1[end].append(start)\n\n        lookup1 = defaultdict(int)\n        self.graph = deepcopy(graph1)\n\n        # we look uptill depth k for graph1\n        for i in range(0, len(edges1) + 1):\n            self.cnt = 0\n            self.seen = set()\n            dfs(i, -1)\n            lookup1[i] = self.cnt\n\n        max_key_2 = max(lookup2.values())\n        \n        ans = []\n        for i in range(0, len(edges1) + 1):\n            ans.append(lookup1[i] + max_key_2)\n        \n        return ans\n```
['Python3']