BFS with Levels || C++ || First thought
Complexity\n- Time complexity: O(N^2 + M^2)\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
# Complexity\n- Time complexity: O(N^2 + M^2)\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:\n    int BFS(int i, vector<vector<int>> &adj, int k, vector<int> vis)\n    {\n        queue<int> q;\n        q.push(i);\n        vis[i] = 1;\n        int sum = 0;\n\n        while(!q.empty() && k>0)\n        {\n            int sz = q.size();\n            while(sz--)\n            {\n                int node = q.front();\n                q.pop();\n\n                for(auto it : adj[node])\n                {\n                    if(!vis[it])\n                    {\n                        sum++;\n                        vis[it] = 1;\n                        q.push(it);\n                    }\n\n                }\n            }\n            k--;\n        }\n\n        return sum;\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<int> vis1(n,0) , vis2(m,0);\n        vector<vector<int>> adj1(n) , adj2(m);\n\n        for(int i=0; i<n-1; i++)\n        {\n            adj1[edges1[i][0]].push_back(edges1[i][1]);\n            adj1[edges1[i][1]].push_back(edges1[i][0]);\n        }\n\n        for(int i=0; i<m-1; i++)\n        {\n            adj2[edges2[i][0]].push_back(edges2[i][1]);\n            adj2[edges2[i][1]].push_back(edges2[i][0]);\n        }\n\n\n        int add = 0;\n        for(int i=0; i<m; i++)\n        {\n            int x = BFS(i,adj2,k-1,vis2); // for graph2 finding the max possible\n            add = max(add,x); // value with one less edge thats why k-1\n        }\n\n        vector<int> answer(n,add+1); // every node is a target of itself\n\n        for(int i=0; i<n; i++)\n        {\n            int sum = BFS(i,adj1,k,vis1);\n            answer[i] += sum;\n            if(k>=1) answer[i]++; // adding 1 for connection to graph2\n        }\n\n        return answer;\n\n    }\n};\n```
['C++']