auto optimize_cpp_stdio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return0; }(); classSolution { public: conststaticint maxn = 1e5 + 10; conststaticint maxm = 1e5 + 10; constint INF = 0x3f3f3f3f; vector<vector<int>> g; vector<int> smallestMissingValueSubtree(vector<int> &parents, vector<int> &nums) { int n = nums.size(); vector<int> ans(n, 1); g.resize(n); for (int i = 0; i < n; ++i) { int u = parents[i]; if (u == -1) continue; int v = i; g[u].emplace_back(v); } // 找 nums[i] = 1 的 i int index = find(nums.begin(), nums.end(), 1) - nums.begin(); if (index == n) return ans; unordered_set<int> vis; // fa 其实表示他的孩子节点 function<void(int u, int fa)> dfs = [&](int u, int fa) { vis.insert(nums[u]); for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (v == fa) continue; dfs(v, u); } }; int mex = 2; int cur = index, nex = -1; while (cur != -1) { dfs(cur, nex); while (vis.count(mex)) ++mex; ans[cur] = mex; nex = cur; cur = parents[cur]; } return ans; } };