Using bfs
\njava []\nclass Solution {\n    public int[] maxTargetNodes(int[][] e1, int[][] e2, int k) {\n         int[]ans=new int[e1.length+1];  \n        if(k==0){\n   
\n```java []\nclass Solution {\n    public int[] maxTargetNodes(int[][] e1, int[][] e2, int k) {\n         int[]ans=new int[e1.length+1];  \n        if(k==0){\n            Arrays.fill(ans,1);\n            return ans;\n        }\n        ArrayList<ArrayList<Integer>>adj1=new ArrayList<>();\n        for(int i=0;i<ans.length;i++){\n            adj1.add(new ArrayList<>());\n        }\n        for(int[]it:e1){\n            adj1.get(it[0]).add(it[1]);\n            adj1.get(it[1]).add(it[0]);\n        }\n         ArrayList<ArrayList<Integer>>adj2=new ArrayList<>();\n        for(int i=0;i<e2.length+1;i++){\n            adj2.add(new ArrayList<>());\n        }\n        for(int[]it:e2){\n            adj2.get(it[0]).add(it[1]);\n            adj2.get(it[1]).add(it[0]);\n        }\n        int max=0;\n        int node=-1;\n        for(int i=0;i<adj2.size();i++){\n          int pp=bfs(i,adj2,k-1);\n            if(pp>max){\n                max=pp;\n                node=i;\n            }\n        }\n        for(int i=0;i<ans.length;i++){\n            ans[i]=2+bfs(i,adj1,k)+max;\n            \n        }\n        return ans;\n    }\n    public int bfs(int r,ArrayList<ArrayList<Integer>>adj,int k){\n        Queue<int[]>x=new ArrayDeque<>();\n        x.offer(new int[]{r,0});\n        int[]vis=new int[adj.size()];\n        vis[r]=0;\n        int ans=0;\n        while(x.size()>0){\n            int[] p=x.poll();\n            vis[p[0]]=1;\n            for(int it:adj.get(p[0])){\n                if(vis[it]==0){\n                  if(p[1]+1<=k){\n                      ans++;\n                      x.offer(new int[]{it,p[1]+1});\n                      vis[it]=1;\n                  } \n                    \n                    \n                }\n            }\n        }\n       \n        return ans;\n    }\n}\n```
['Java']