Simple DFS - C++ - O(k(m+n)) -> O(m+n)
Code\ncpp []\nclass Solution {\npublic:\n    void bfs2(int s, vector<int>& ans, vector<vector<int>>& adj, int k){\n        vector<int> vis(adj.size(), 0);\n    
# Code\n```cpp []\nclass Solution {\npublic:\n    void bfs2(int s, vector<int>& ans, vector<vector<int>>& adj, int k){\n        vector<int> vis(adj.size(), 0);\n        queue<pair<int, int>> q;\n        q.push({s, 0});\n        vis[s]=1;\n        while(!q.empty()){\n            pair<int, int> top = q.front();\n            q.pop();\n            int node = top.first;\n            int dist = top.second;\n            if(dist<=k){\n                ans[s]++;\n            }else{\n                continue;\n            }\n            for(int ngb: adj[node]){\n                if(ngb==s || vis[ngb]!=0) continue;\n                q.push({ngb, dist+1});\n                vis[ngb]=1;\n            }\n            vis[node]=2;\n        }\n    }\n    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2, int k) {\n        int n = edges1.size()+1;\n        int m = edges2.size()+1;\n        vector<vector<int>> adj1(n, vector<int>()), adj2(m, vector<int>());\n        for(auto edge: edges1){\n            int u = edge[0];\n            int v = edge[1];\n            adj1[u].push_back(v);\n            adj1[v].push_back(u);\n        }\n        for(auto edge: edges2){\n            int u = edge[0];\n            int v = edge[1];\n            adj2[u].push_back(v);\n            adj2[v].push_back(u);\n        }\n        vector<int> t2kminus1(m, 0);\n        int maxT2 = 0;\n        for(int i=0; i<m; i++){\n            bfs2(i, t2kminus1, adj2, k-1);\n            maxT2 = max(maxT2, t2kminus1[i]);\n        }\n        cout<<maxT2<<endl;\n        vector<int> ans(n, maxT2);\n        for(int i=0; i<n; i++){\n            bfs2(i, ans, adj1, k);\n        }\n        return ans;\n    }\n};\n```
['C++']