[C++] Union-Find + DFS
cpp\nstruct dsu {\n  vector<int> parent, rank;\n  dsu(int n) : parent(n), rank(n, 1) {\n    for (int i = 0; i < n; i++)\n      parent[i] = i;\n  }\n  int find(i
```cpp\nstruct dsu {\n  vector<int> parent, rank;\n  dsu(int n) : parent(n), rank(n, 1) {\n    for (int i = 0; i < n; i++)\n      parent[i] = i;\n  }\n  int find(int i) {\n    if (parent[i] == i)\n      return i;\n    return parent[i] = find(parent[i]);\n  }\n  void unite(int i, int j) {\n    if (find(i) != find(j)) {\n      int repi = find(i);\n      int repj = find(j);\n      if (rank[repi] < rank[repj])\n        swap(repi, repj);\n      parent[repj] = repi;\n      rank[repi] += int(rank[repi] == rank[repj]);\n    }\n  }\n};\n\nclass Solution {\npublic:\n  vector<int> smallestMissingValueSubtree(vector<int> &parents, vector<int> &nums) {\n    int n = parents.size();\n\n    vector<vector<int>> childrens(n);\n    for (int i = 1; i < n; i++) {\n      childrens[parents[i]].push_back(i);\n    }\n\n    dsu ds(200001);\n    for (int i = 0; i < n; i++) {\n      ds.unite(nums[i], i + 100001);\n    }\n\n    vector<int> ans(n);\n    auto dfs = [&](auto &self, int cur) -> int {\n      int mex = 1;\n      for (int children : childrens[cur]) {\n        int children_mex = self(self, children);\n        mex = max(mex, children_mex);\n        ds.unite(children + 100001, cur + 100001);\n      }\n      for (; mex <= 100000 && ds.find(mex) == ds.find(cur + 100001); mex++) {\n      }\n      return ans[cur] = mex;\n    };\n    dfs(dfs, 0);\n\n    return ans;\n  }\n};\n```
['Depth-First Search', 'Union Find', 'C']