解题报告;
直接dp。注意mx也得longlong
AC代码:
class Solution {
public:vector<int> vv[200005];int sum[200005];long long ans[200005];int n;void dfs(int x) {ans[x] = 1; sum[x] = 1;for(int i = 0; i<vv[x].size(); i++) {dfs(vv[x][i]);sum[x] += sum[vv[x][i]];ans[x] *= sum[vv[x][i]];}if(n-sum[x] > 0) ans[x] *= (n-sum[x]);}int countHighestScoreNodes(vector<int>& parents) {n = parents.size();for(int i = 1; i<n; i++) {vv[parents[i]].push_back(i);}dfs(0);long long mx = 0;int cnt = 0;for(int i = 0; i<=n; i++) {if(ans[i] > mx) {cnt = 1;mx = ans[i];} else if(ans[i] == mx) {cnt ++;}}return cnt;}
};