Easy Method Using BFS
Bold# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n-
**Bold**# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N+M)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N+M)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\nvector<int> bfs(vector<int>adj[],int n,int k) \n{\n    vector<int>ans(n,0); \n    for(int i=0;i<n;i++) \n        {\n            int c=0; \n           vector<int>boo(n,0); \n            queue<pair<int,int>>q; \n            q.push({i,0}); \n            boo[i]=1; \n            while(!q.empty()) \n                {\n                    int node=q.front().first; \n                    int d=q.front().second;\n                    q.pop(); \n                    if(d>k) continue; \n                    c++; \n                    for(int j:adj[node]) \n                        {\n                            if(boo[j]==0) \n                            {\n                                boo[j]=1; \n                                q.push({j,d+1}); \n                            }\n                                \n                        }\n                }\n            ans[i]=c; \n        }\n    return ans; \n}\n    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2, int k) {\n        // converting Edges to Adj Matrix; \n        int n=edges1.size()+1; \n        vector<int>adj1[1001]; \n        /* \n            0->1,2 \n            1->0 \n            2->0,3,4 \n            3->2        */ \n        for(auto i:edges1) \n            {\n                adj1[i[0]].push_back(i[1]); \n                adj1[i[1]].push_back(i[0]); \n            }\n        int m=edges2.size()+1; \n        vector<int>adj2[1001]; \n        for(auto i:edges2) \n            {\n                adj2[i[0]].push_back(i[1]); \n                adj2[i[1]].push_back(i[0]); \n            }\n        /* ,\n            0->1,2,3 \n            1->0,4 \n            2->0,7\n            3->0 \n            4->1,5,6\n            5->4\n            6->4\n            7->2 */ \n\n        vector<int>ans1=bfs(adj1,n,k); \n        vector<int>ans2=bfs(adj2,m,k-1); \n        vector<int>ans; \n        for(int i=0;i<n;i++) \n            {\n                int jabir=0; \n                for(int j=0;j<m;j++) \n                    {\n                        jabir=max(jabir,ans1[i]+ans2[j]); \n                    }\n                ans.push_back(jabir); \n            }\n        return ans; \n    }\n};\n```
['C++']