Java Solution BFS
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
```java []
class Solution {
    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
        Map<Integer, List<Integer>> graph1 = new HashMap<>();
        Map<Integer, List<Integer>> graph2 = new HashMap<>();
        buildgraph(edges1, graph1);
        buildgraph(edges2, graph2);
        int maxedge1 = 0;
        for(int[] edge : edges1){
            maxedge1 = Math.max(maxedge1, edge[0]);
            maxedge1 = Math.max(maxedge1, edge[1]);
        }
        maxedge1++;

        int maxedge2 = 0;
        for(int[] edge : edges2){
            maxedge2 = Math.max(maxedge2, edge[0]);
            maxedge2 = Math.max(maxedge2, edge[1]);
        }
        maxedge2++;

        int[][] tree1reachable = computereachable(graph1, maxedge1, k);
        int[][] tree2reachable = computereachable(graph2, maxedge2, k - 1);
        
        int[] tree2maxreachable = computetree2maxreachable(tree2reachable, maxedge2, k);
        int[] result = new int[maxedge1];
        for(int i = 0; i < maxedge1; i++){
            int counttree1 = (k < tree1reachable[i].length) ? tree1reachable[i][k] : tree1reachable[i][tree1reachable[i].length - 1];

            int counttree2 = k >= 1 ? tree2maxreachable[k - 1] : 0;
            result[i] = counttree1 + counttree2;
        }
        return result;
    }
    private void buildgraph(int[][] edges, Map<Integer, List<Integer>> graph){
        for(int[] edge : edges){
            int from = edge[0];
            int to = edge[1];
            graph.putIfAbsent(from, new ArrayList<>());
            graph.get(from).add(to);
            graph.putIfAbsent(to, new ArrayList<>());
            graph.get(to).add(from);
        }
    }
    private int[][] computereachable(Map<Integer, List<Integer>> graph, int numnodes, int maxdepth){
        int[][] reachable = new int[numnodes][maxdepth + 1];
        for(int i = 0; i < numnodes; i++){
            reachable[i] = bfscount(graph, i, maxdepth);
        }
        return reachable;
    }
    private int[] bfscount(Map<Integer, List<Integer>> graph, int start, int maxdepth){
        if(maxdepth < 0){
            return new int[0];
        }
        int[] count = new int[maxdepth + 1];
        Queue<Integer> queue = new ArrayDeque<>();
        queue.offer(start);
        Set<Integer> visited = new HashSet<>();
        visited.add(start);
        count[0] = 1;
        int depth = 1;
        while(!queue.isEmpty() && depth <= maxdepth){
            int size = queue.size();
            for(int i = 0; i < size; i++){
                int current = queue.poll();
                List<Integer> list = graph.get(current);
                if(list == null){
                    continue;
                }
                for(int next : list){
                    if(visited.contains(next)){
                        continue;
                    }
                    visited.add(next);
                    count[depth] += 1;
                    queue.offer(next);
                }
            }
            depth++;
        }
        for(int i = 1; i <= maxdepth; i++){
            count[i] += count[i - 1];
        }
        return count;
    }
    private int[] computetree2maxreachable(int[][] tree2reachable, int m, int k){
        int[] maxreachable = new int[k];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < k; j++){
                if(j < tree2reachable[i].length){
                    maxreachable[j] = Math.max(maxreachable[j], tree2reachable[i][j]);
                }
            }
        }
        return maxreachable;
    }
}
```
['Java']