C++ simple dfs with explanation
class Solution {\npublic:\n   \n    \n    int dfs(vector adj[], int n,int i, vector&dp)\n    {\n        int ans=1;\n       \n       // ans+=adj[i].size();\n    
class Solution {\npublic:\n   \n    \n    int dfs(vector<int> adj[], int n,int i, vector<int>&dp)\n    {\n        int ans=1;\n       \n       // ans+=adj[i].size();\n        \n        for(auto it:adj[i])\n        {\n           ans+= dfs(adj,n,it,dp);\n        }\n        \n        return dp[i]=ans;\n    }\n    int countHighestScoreNodes(vector<int>& parents) {\n       \n        int n=parents.size();\n        \n        vector<int>dp(n+1,-1);\n        \n       vector<int>adj[n+1];\n        \n       for(int i=0;i<n;i++)\n       {\n           if(parents[i]!=-1)\n           {\n               adj[parents[i]].push_back(i);\n           }\n       }\n        long long mn=0;\n        int id=0;\n        \n        dfs(adj,n,0,dp); // dfs for calculating the size of each subtree\n        \n        \n       for(int i=0;i<n;i++)\n       {\n           long long a=0,b=0,prod=0;\n           \n           if(adj[i].size()==2)    // if it has two size thn there will be three comp in prod\n           {\n              a= dp[adj[i][0]];\n              b= dp[adj[i][1]];\n               \n              prod=a*b*(n-a-b-1);\n              if(i==0){ prod=a*b;}\n           }\n           else if(adj[i].size()==1){  // else 2 comp would be there\n              \n               a=dp[adj[i][0]];\n               prod=a*(n-a-1);\n               if(i==0){ prod=a;}\n           }\n           else{                 // if no size thn must be leaf, so size is n-1\n              prod=n-1; \n           }\n           \n         //  cout<<a<<" "<<b<<" "<<prod<<endl;\n           if(prod>mn){\n               mn=prod;           // cal mx prod\n               id=1;          // if new mx prod found,initiase it ti 1, as this is only elemnt\n           }\n           else if(prod==mn)\n           {\n               id++;            // else increase its val\n           }\n           \n       }\n        \n        \n        return id;\n    }\n};
[]