BFS solution
Intuition\n Describe your first thoughts on how to solve this problem. \nGot this problem in the contest\nThis is a Graph problem\n\n# Approach\n Describe your 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGot this problem in the contest\nThis is a Graph problem\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCan be broken down to below steps:\n1. Generate adjacency lists, adj1 and adj2\n2. Find max of **no of nodes at distance <= k from every nodes in adj2** \n3. Find **no of nodes at distance <= k for each node in adj1** in an array **say, ans[ ]**\n4. Final answer for each node of **adj2 = ans[i] + (result from step 2)**\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        \n        List<List<Integer>> adj1 = generateGraph(edges1);\n        List<List<Integer>> adj2 = generateGraph(edges2);\n        \n        int n2 = edges2.length + 1;\n        int n1 = edges1.length + 1;\n        \n        // no of nodes at dist <= k\n        int[] ans = new int[n1];\n\n        // find max of no:of nodes at distance <= k among every nodes in second graph\n        // (since g1 can be connected to any nodes of g2)\n        int max2 = -1;\n        for (int j = 0; j < n2; j++) {\n            max2 = Math.max(max2, getNoOfNodes(adj2, j, k-1));\n        }\n\n        // find no of nodes at distance <= k from each node of first graph\n        // add max2 to each values\n        for (int i = 0; i < n1; i++) {\n            ans[i] = getNoOfNodes(adj1, i, k) + max2;\n        }\n\n        return ans;\n    }\n\n    /**\n     * Get no of nodes at distance <= k from given root\n     * uses BFS. We can also use dfs with less lines\n     */\n    private int getNoOfNodes(List<List<Integer>> adj, int root, int k) {\n        Queue<Integer> queue = new LinkedList<>();\n        queue.add(root);\n        boolean[] visited = new boolean[adj.size()];\n        visited[root] = true;\n\n        int count = 0;\n        int dist = 0;\n        while (!queue.isEmpty()) {\n            int size = queue.size();\n            if (dist <= k) {\n                count += size;\n            } else {\n                break;\n            }\n            for (int i = 0; i < size; i++) {\n                int curr = queue.remove();\n                List<Integer> adjNodes = adj.get(curr);\n                for (int adjNode: adjNodes) {\n                    if (visited[adjNode]) continue;\n                    visited[adjNode] = true;\n                    queue.add(adjNode);\n                }\n            }\n            dist++;\n        }\n        return count;\n    }\n\n    private List<List<Integer>> generateGraph(int[][] edges) {\n        int n = edges.length + 1;\n        List<List<Integer>> adj = new ArrayList<>();\n        \n        for (int i = 0; i < n; i++) adj.add(new ArrayList<>());\n        \n        for (int[] edge: edges) {\n            adj.get(edge[0]).add(edge[1]);\n            adj.get(edge[1]).add(edge[0]);\n        }\n\n        return adj;\n    }\n}\n```
['Java']