<p>There is a <strong>binary</strong> tree rooted at <code>0</code> consisting of <code>n</code> nodes. The nodes are labeled from <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> integer array <code>parents</code> representing the tree, where <code>parents[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parents[0] == -1</code>.</p>

<p>Each node has a <strong>score</strong>. To find the score of a node, consider if the node and the edges connected to it were <strong>removed</strong>. The tree would become one or more <strong>non-empty</strong> subtrees. The <strong>size</strong> of a subtree is the number of the nodes in it. The <strong>score</strong> of the node is the <strong>product of the sizes</strong> of all those subtrees.</p>

<p>Return <em>the <strong>number</strong> of nodes that have the <strong>highest score</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/03/example-1.png" style="width: 604px; height: 266px;" />
<pre>
<strong>Input:</strong> parents = [-1,2,0,2,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The score of node 0 is: 3 * 1 = 3
- The score of node 1 is: 4 = 4
- The score of node 2 is: 1 * 1 * 2 = 2
- The score of node 3 is: 4 = 4
- The score of node 4 is: 4 = 4
The highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="example-2" src="https://assets.leetcode.com/uploads/2021/10/03/example-2.png" style="width: 95px; height: 143px;" />
<pre>
<strong>Input:</strong> parents = [-1,2,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
- The score of node 0 is: 2 = 2
- The score of node 1 is: 2 = 2
- The score of node 2 is: 1 * 1 = 1
The highest score is 2, and two nodes (node 0 and node 1) have the highest score.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == parents.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>parents[0] == -1</code></li>
	<li><code>0 &lt;= parents[i] &lt;= n - 1</code> for <code>i != 0</code></li>
	<li><code>parents</code> represents a valid binary tree.</li>
</ul>

from collections import defaultdict as dd
'''
        # nodes_in_subtree = dd(lambda:0)
        # pending = [i for i in range(len(parents)) if i not in parents]
        # while(pending):
        #     curr = pending[0]
        #     pending = pending[1:]
        #     nodes_in_subtree[curr]+=1
        #     parent = parents[curr]
        #     if parent==-1:
        #         continue
        #     pending.append(parent)
        
        # for i in range(len(parents)):
        #     if i in parents:
        #         nodes_in_subtree[i]+=1
        

        # children = dd(lambda:[])
        # for i in range(len(parents)):
        #     if parents[i]!=-1:
        #         children[parents[i]].append(i)

        # node_ans = [0 for _ in range(len(parents))]
        # for i in range(len(parents)):
        #     if parents[i]!=-1:
        #         curr_ans = 1
        #         child_sum = 0
        #         for c in children:
        #             curr_ans*=nodes_in_subtree[c]
        #             child_sum+=nodes_in_subtree[c]
        #         curr_ans*=(len(parents)-child_sum)
        #         node_ans[i] = curr_ans
        #     else:
        #         curr_ans = 1
        #         child_sum = 0
        #         for c in children:
        #             curr_ans*=nodes_in_subtree[c]          
        # print(node_ans)
        # return node_ans.count(max(node_ans))
'''


class Solution:
    def countHighestScoreNodes(self, parents: List[int]) -> int:
        n = len(parents)
        tree = defaultdict(list)
        for c, p in enumerate(parents):
            tree[p].append(c)

        max_score = 0

        def cal_score(child_sizes):
            score = 1
            for size in child_sizes:
                score *= size
            return score * ((n - sum(child_sizes) - 1) or 1)
            
        def subtree_size(node):
            child_sizes = []
            size = 1
            if tree[node]:
                for child in tree[node]:
                    child_sizes.append(subtree_size(child))
                size += sum(child_sizes)
            nonlocal scores
            scores.append(cal_score(child_sizes))
            return size

        scores = []
        subtree_size(0)
        # return max_score
        return scores.count(max(scores))

66692