Solution
Intuition\nThe very first thought that comes in the mind is that since we have to find number of all nodes upto level k from node u so it can be level order tra
# Intuition\nThe very first thought that comes in the mind is that since we have to find number of all nodes upto level k from node u so it can be level order traversing.\n\n# Approach\nAs we can understand this problem can further be break into two subproblems the first one is for every node in first tree we have to find number of all nodes upto level k. The other subproblem is we have to find a node in second tree such that it has maximum number of nodes upto level k-1(It is till level k-1 because 1 connection is always require to connect the both trees).It can be seem very easily that both subproblem are independent as for any node i from first tree we can always select the node j from second tree for maximum nodes(j is the solution for our second subproblem).We can find solutions to both subproblem by applying BFS. For second subproblem we can apply BFS for every node and keep track of maximum nodes so far.The constraints given in the problem allows us to do so.Finally we can add the answers of both these subproblems to finally get the answer.\n\n# Complexity\n- Time complexity:\nWe are applying BFS for every node so in worstscenario time complexity is O(n^2+m^2).\n\n- Space complexity:\nSince the maximum number of vertices in the queue is bounded by the number of vertices in the graph, the space complexity of the queue is O(max(n,m)).\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\nint  help(vector<vector<int>> &v,int k,int i)\n{\n\n\n\tvector<bool> vis(v.size(),false);\n\n\tint sum=0;\n\tint level=0;\n\n\t\tif(k<level)\n\t\treturn sum;\n\n\t\t\n\t\tqueue<int> q;\n\t\tq.push(i);\n\t\tvis[i]=true;\n\t\tsum++;\n\t\t\n\n\t\twhile(!q.empty())\n\t\t{\n\t\tif(k==level)\n\t\t\treturn sum;\n\n\n\t\tint x=q.size();\n\t\twhile(x--)\n\t\t{\n\t\tint f=q.front();\n\t\tq.pop();\n\t\tfor(auto j:v[f])\n\t\t{\n\t\tif(!vis[j])\n\t\t{\n\t\n\t\tq.push(j);\n\t\tvis[j]=true;\n\t\tsum++;\n\t\n\t\t}\n\t\n\t\n\t\t}\n\t\t}\n\t\tlevel++;\n\n\n\n   \n\t\t}\n\t\treturn sum;\n}\n    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2, int k) {\nvector<vector<int>> t1(edges1.size()+1,vector<int>()),t2(edges2.size()+1,vector<int>());\n\nfor(auto i:edges1)\n{\n    t1[i[0]].push_back(i[1]);\n    t1[i[1]].push_back(i[0]);\n\n}\nfor(auto i:edges2)\n{\n    t2[i[0]].push_back(i[1]);\n    t2[i[1]].push_back(i[0]);\n\n}\nvector<int> ans(edges1.size()+1);\nint maxi=0;\n\nint loc;\nfor(int i=0;i<t2.size();i++)\n{\n   loc=help(t2,k-1,i);\n   maxi=max(maxi,loc);\n    \n\n}\ncout<<maxi;\nfor(int i=0;i<t1.size();i++)\n{\nans[i]=help(t1,k,i);\nans[i]+=maxi;\n\n}\n\nreturn ans;\n        \n    }\n};\n```
['Tree', 'Breadth-First Search', 'C++']