统计每个子树的个数就行
当前节点的value = 它的所有子树的节点个数 + 根节点树节点的个数 - 当前节点子树节点的个数
class Solution { public: vector<int> v[100010]; int num[100010]; long long ret[100010]; int cnt; int dfs(int u) { int len = v[u].size(); int cnt1 = 0, cnt2 = 0; for(int i = 0; i < len; i++) { if(i == 0) cnt1 = dfs(v[u][i]); else cnt2 = dfs(v[u][i]); } num[u] = cnt1 + cnt2 + 1; return num[u]; } int countHighestScoreNodes(vector<int>& parents) { int n = parents.size(); for(int i = 0; i < n; i++) if(i != 0) v[parents[i]].push_back(i); int k = dfs(0); long long max_v = -1, sum; for(int i = 0; i < n; i++) { sum = 1; if(i == 0) { int len2 = v[i].size(); for(int j = 0; j < len2; j++) sum *= (long long)num[v[i][j]]; } else { int len2 = v[i].size(); sum *= (long long)(num[0] - num[i]); for(int j = 0; j < len2; j++) sum *= (long long)num[v[i][j]]; } ret[i] = sum; max_v = max(max_v, sum); } int ans = 0; for(int i = 0; i < n; i++) if(ret[i] == max_v) ans++; return ans; } };