题解 | #好子树#
好子树
https://www.nowcoder.com/practice/aca56bd0c9414c71b94d3a2851f4e0e7
#include <bits/stdc++.h> using namespace std; int find_min(vector<vector<int>>& e, vector<int>& tree, int root) { int mn = tree[root]; for (auto c : e[root]) { mn = min(find_min(e, tree, c), mn); } return mn; } int find_max(vector<vector<int>>& e, vector<int>& tree, int root) { int mx = tree[root]; for (auto c : e[root]) { mx = max(find_max(e, tree, c), mx); } return mx; } int main() { int n, r, c; cin >> n; vector<int> tree(n + 1); for (int i = 1; i <= n; i++) cin >> tree[i]; vector<vector<int>> e(n + 1, vector<int>()); for (int i = 1; i < n; i++) { cin >> r >> c; e[r].push_back(c); } vector<int> mn, mx; mn.push_back(-1); mx.push_back(-1); for (int i = 1; i <= n; i++) { mn.push_back(find_min(e, tree, i)); mx.push_back(find_max(e, tree, i)); } vector<int> ret; for (int i = 1; i <= n; i++) { if ((mx[i] - mn[i]) % 2) { ret.push_back(i); } } cout << ret.size() << endl; for_each(ret.begin(), ret.end(), [](int val)->void{cout << val << " ";}); return 0; }