bfs on all nodes: a working solution
Intuition\n- the targets reachable from u with at most k edges,is obviously bfs doable, with O(N^2)\n\n \n\n# Code\ncpp []\nclass Solution {\npublic:\n    vecto
# Intuition\n- the targets reachable from `u` with at most `k` edges,is obviously `bfs` doable, with `O(N^2)`\n\n \n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2, const int k) {\n        int n = edges1.size() + 1; vector<unordered_set<int>> adj1(n);\n        int m = edges2.size() + 1; vector<unordered_set<int>> adj2(m);\n        auto mkadj = [](vector<vector<int>>& edges,vector<unordered_set<int>>& adj) {\n            for(auto const& e : edges) {\n                int u = e[0], v = e[1];\n                adj[u].insert(v);\n                adj[v].insert(u);\n            }\n        };\n        mkadj(edges1, adj1);\n        mkadj(edges2, adj2);\n        \n        auto bfs = [](vector<unordered_set<int>>const& adj, int u, int k) {\n            if(k < 0) return 0;\n            int reaches = 0;\n            queue<int> q;\n            q.push(u);\n            unordered_set<int> visited;\n            while(!q.empty() && k-- >= 0) {\n                for(int sz = q.size(); sz; --sz) {\n                    int a = q.front();\n                    q.pop();\n                    ++reaches;\n                    visited.insert(a);\n                    for(auto b : adj[a]) {\n                        if(!visited.contains(b)) \n                            q.push(b);\n                    }\n                }\n            }\n            return reaches;\n        };\n        vector<int> t1(n); for(int u = 0; u < n; ++u) t1[u] = bfs(adj1, u, k);\n        vector<int> t2(m); for(int u = 0; u < m; ++u) t2[u] = bfs(adj2, u, k-1);\n        int maxt2 = *max_element(t2.begin(), t2.end());\n        for(auto & a : t1) a += maxt2;\n        return t1;\n    }\n};\n```
['C++']