Count Nodes With the Highest Score | C++ | SubTree Size + Maths | DFS
\nclass Solution {\npublic:\n    int countHighestScoreNodes(vector<int>& p) {\n        int n = p.size();\n        vector<int> tree[n], subTreeSize(n, 0), vis(n,
```\nclass Solution {\npublic:\n    int countHighestScoreNodes(vector<int>& p) {\n        int n = p.size();\n        vector<int> tree[n], subTreeSize(n, 0), vis(n, 0);\n        for(int i = 0; i < n; i++){\n            if(p[i] != -1){\n                tree[p[i]].push_back(i);\n            }\n        }\n        \n        function<int(int)> dfs=[&](int u)-> int{\n            vis[u] = 1;\n            int currSize = 1;\n            for(auto& next : tree[u]){\n                if(!vis[next]){\n                    currSize += dfs(next);\n                }\n            }\n            subTreeSize[u] = currSize;\n            return currSize;\n        };\n        \n        dfs(0);\n        \n        long ans = 0;\n        unordered_map<long ,int> freq;\n        for(int i = 0; i < n; i++){\n            long val = 1, sum = 0;\n            for(auto& c : tree[i]){\n                if(p[c] == i) val *= subTreeSize[c], sum += subTreeSize[c];\n            }\n            val *= max(1L, n - sum - 1);\n            ans = max(ans, val);\n            freq[val]++;\n        }\n        \n        return freq[ans];\n    }\n};\n```
[]