luogu#P9233. [蓝桥杯 2023 省 A] 颜色平衡树
[蓝桥杯 2023 省 A] 颜色平衡树
Problem Description
Given a tree with nodes numbered from to , where node is the root. Each node has a color .
If, in a tree, the number of nodes for every color that appears is the same, then we call it a color-balanced tree.
Find how many subtrees of this tree are color-balanced trees.
Input Format
The first line contains an integer , representing the number of nodes in the tree.
The next lines each contain two integers , separated by a space, representing the color of node and the index of its parent node.
In particular, the input guarantees that is , meaning node has no parent. The input is guaranteed to form a tree.
Output Format
Output one line containing an integer, representing the answer.
6
2 0
2 1
1 2
3 3
3 4
1 4
4
Hint
Sample Explanation
The subtrees rooted at nodes are color-balanced trees.
Constraints
For of the testdata, , .
For of the testdata, , .
For all testdata, , , .
Translated by ChatGPT 5