public int countHighestScoreNodes(int[] parents) {
        int n = parents.length;
        long[] count = new long[n];
        List<Integer>[] G = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            G[i] = new ArrayList();
        }
        for (int i = 1; i < n; i++) {
            G[i].add(parents[i]);
            G[parents[i]].add(i);
        }
        long max = 0;
        int ans = 0;
        boolean[] marked = new boolean[n];
        dfs(G, marked, count, 0);
        for (int i = 0; i < n; i++) {
            int cnt = 1;
            for (int w  : G[i]) {
                if (parents[i] == w) {
                    cnt *= n - count[i];
                }else{
                    cnt *= count[w];
                }
            }
            if (cnt > max) {
                max = cnt;
                ans = 1;
            } else if (cnt == max) {
                ans++;
            }
        }
        return ans;
    }

    public int  dfs(List<Integer>[] G,boolean[] marked,long[] count, int v) {
        int cnt = 1;
        marked[v] = true;
        for (int w : G[v]) {
            if (!marked[w]) {
                cnt += dfs(G, marked, count, w);
            }
        }
        count[v] = cnt;
        return cnt;
    }