LC5908-统计最高分的节点数目

5908. 统计最高分的节点数目

dfs

using ll = long long;
class Solution {
public:
    ll n, res, ans;
    ll dfs(vector<vector<int>>& G, int root){
        ll cnt = 1,tmp = 1;
        for(auto & e : G[root]){
            ll c = dfs(G,e);
            cnt += c;tmp *= c;
        }
        tmp *= 1ll * max(n - cnt, 1ll);
        if(tmp > res)res = tmp, ans = 1;
        else if(tmp == res) ans ++;
        return cnt;
    }
    int countHighestScoreNodes(vector<int>& parents) {
        res = ans = 0;n = parents.size();
        vector<vector<int>>G(n);
        for(int i = 1; i < parents.size(); ++i)G[parents[i]].push_back(i);
        dfs(G,0);
        return ans;
    }
};
posted @ 2021-10-24 16:05  Ivessas  阅读(26)  评论(0)    收藏  举报