Disjoint set union, 90% fast
\nclass Solution {\npublic:\n    vector<int> ans;\n    vector<int> dsu;\n    int find( int node){\n        if(dsu[node]!=node){\n            return dsu[node]=fi
```\nclass Solution {\npublic:\n    vector<int> ans;\n    vector<int> dsu;\n    int find( int node){\n        if(dsu[node]!=node){\n            return dsu[node]=find(dsu[node]);\n        }\n        return node;\n    }\n    int dfs(vector<vector<int>>& alist, vector<int>&nums, int node){\n        \n        if(alist[node].size()==0) {\n            if(nums[node]!=1) {\n                ans[node]=1;\n                return 1;\n            }\n            ans[node]=2;\n            return 2;\n        }\n        \n        int temp;\n        int chosen=INT_MIN;\n        for(int child: alist[node]){\n            temp= dfs(alist, nums, child);\n            dsu[nums[child]]= nums[node];\n            chosen= max(chosen, temp);\n        }\n\n        while( find(chosen)==nums[node]){\n            chosen++;\n        }\n        ans[node]=chosen;\n        return chosen;\n    }\n    vector<int> smallestMissingValueSubtree(vector<int>& parents, vector<int>& nums) {\n        \n        int n=parents.size();\n        dsu.push_back(0);\n        ans.resize(n);\n        \n        for(int i=1; i<=100000; i++){\n            dsu.push_back(i);\n        }\n        \n        vector<vector<int>> alist(n,vector<int>());\n        \n        for(int i=1; i<n; i++){\n            alist[parents[i]].push_back(i);\n        }\n        //cout<<"h";\n        dfs(alist, nums,0);\n        return ans;\n    }\n};\n```
[]