Easy To Understand C++ | Merging Sets
\nclass Solution {\npublic:\n    set<int> ss[100005];\n    void dfs(int s,vector<vector<int>> &adj,vector<int> &ans,vector<int>&nums)\n    {\n        for(int i=
```\nclass Solution {\npublic:\n    set<int> ss[100005];\n    void dfs(int s,vector<vector<int>> &adj,vector<int> &ans,vector<int>&nums)\n    {\n        for(int i=0;i<adj[s].size();i++)\n        {\n            dfs(adj[s][i],adj,ans,nums);\n        }\n        \n        ss[s].insert(nums[s]);\n        ans[s]=1;\n        for(int i=0;i<adj[s].size();i++)\n        {\n            ans[s]=max(ans[s],ans[adj[s][i]]);\n            if(ss[s].size()<ss[adj[s][i]].size())\n                swap(ss[s],ss[adj[s][i]]);\n            for(auto it:ss[adj[s][i]])\n            {\n                ss[s].insert(it); \n            }\n            \n        }\n        while(ss[s].find(ans[s])!=ss[s].end())\n            ans[s]++;\n    }\n    vector<int> smallestMissingValueSubtree(vector<int>& p, vector<int>& nums) {\n        \n        \n        vector<vector<int>> adj(nums.size()+1);\n        vector<int> ans(nums.size());\n        for(int i=0;i<=nums.size();i++)\n        {\n            ss[i].clear();            \n        }\n        \n        for(int i=1;i<p.size();i++)\n        {\n            adj[p[i]].push_back(i);\n        }\n        \n        dfs(0,adj,ans,nums);\n        return ans;\n        \n        \n    }\n};\n```
[]