🔥Simple DFS⚡ || ✅Clean & Best Code💯
\n\n# Complexity\n\n- Time complexity:\nO(m * m + n * n)\n\n- Space complexity:\nO(m + v)\n\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\ncpp []\nclass S
\n\n# Complexity\n```\n- Time complexity:\nO(m * m + n * n)\n\n- Space complexity:\nO(m + v)\n```\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n```cpp []\nclass Solution {\npublic:\n    int cnt = 0;\n    void dfs(int node, vector<vector<int>>& adj, int k, int val, vector<bool>& vis){\n        vis[node] = true;\n        if(val > k)\n            return ;\n        cnt++;\n        for(auto it : adj[node]){\n            if(!vis[it])\n                dfs(it, adj, k, val + 1, vis);\n        }\n    }\n\n    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2, int k) {\n        int n = edges1.size() + 1, m = edges2.size() + 1;\n        vector<int> ans(n);\n        vector<vector<int>> adj1(n), adj2(m);\n        for(auto it : edges1){\n            int x = it[0], y = it[1];\n            adj1[x].push_back(y);\n            adj1[y].push_back(x);\n        }\n        for(auto it : edges2){\n            int x = it[0], y = it[1];\n            adj2[x].push_back(y);\n            adj2[y].push_back(x);\n        }\n        int maxi = 0;\n        for(int i = 0 ; i < m ; i++){\n            cnt = 0;\n            vector<bool> vis(m);\n            dfs(i, adj2, k - 1, 0, vis);\n            maxi = max(maxi, cnt);\n        }\n        for(int i = 0 ; i < n ; i++){\n            cnt = 0;\n            vector<bool> vis(n);\n            dfs(i, adj1, k, 0, vis);\n            ans[i] = cnt + maxi;\n        }\n        return ans;\n    }\n};\n```
['Tree', 'Depth-First Search', 'C++']