luogu#P9233. [蓝桥杯 2023 省 A] 颜色平衡树

[蓝桥杯 2023 省 A] 颜色平衡树

Problem Description

Given a tree with nodes numbered from 11 to nn, where node 11 is the root. Each node has a color CiC_i.

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 nn, representing the number of nodes in the tree.

The next nn lines each contain two integers Ci,FiC_i, F_i, separated by a space, representing the color of node ii and the index of its parent node.

In particular, the input guarantees that F1F_1 is 00, meaning node 11 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 1,3,5,61, 3, 5, 6 are color-balanced trees.

Constraints

For 30%30\% of the testdata, n200n \leq 200, Ci200C_i \leq 200.

For 60%60\% of the testdata, n5000n \leq 5000, Ci5000C_i \leq 5000.

For all testdata, 1n2×1051 \leq n \leq 2\times 10 ^ 5, 1Ci2×1051 \leq C_i \leq 2\times 10 ^ 5, 0Fi<i0 \leq F_i<i.

Translated by ChatGPT 5