#P4333. [JSOI2010] 游戏
[JSOI2010] 游戏
Description
Little L, Little H, and Little X from the JSOI training team, after intense training, always like to play a game called “number picking” to relax.
This is a single-player game. You only need a blank sheet of paper and a pen. The player randomly writes a line of integers on the paper to form a sequence, and then the game starts.
Each time, the player chooses one number from the left end or the right end of the original sequence, crosses it out from the original sequence, and writes it on the next line. After all numbers in the original sequence are crossed out, a new sequence of length appears on the second line, denoted as . The score of sequence is computed as follows:
$$P=S_1\times 5^0+S_2\times 5^1+\cdots+S_n\times 5^{n-1}$$After computing the score , convert it to binary. If the last three bits are , the player wins the game; otherwise, the player loses.
After playing this game many times, they discovered an important fact: for some randomly written sequences, no matter how you play, it is impossible to win. Such sequences are called “bad sequences”, and the other sequences are called “good sequences”.
Although this game is very fun, it has one drawback: before each game, it takes a lot of time to write down the random sequence. This has always bothered them.
Until the night before this year’s NOI Qualifier, Little L came up with an amazing idea and solved this problem at once. They first draw a huge unrooted tree (with nodes) on the paper, and write an integer on each node. When they want to play the game, the player just chooses any two nodes, finds the unique path connecting these two nodes, and lists the integers written on all nodes along the path (including both endpoints) in order of the path. This gives a sequence, and then they can play the game on this sequence. If the two endpoints are nodes and , the obtained sequence is denoted briefly as . Of course, as mentioned above, the sequence can also be either a “good sequence” or a “bad sequence”.
They found this improvement really makes things much more convenient. Moreover, it brings some new fun to the game. For example, Little X claimed that he found an important rule: the property of sequences is transitive. That is, for any distinct :
-
If is a good sequence and is a good sequence, then is a good sequence.
-
If is a bad sequence and is a bad sequence, then is a bad sequence.
This conclusion is surprisingly elegant, but soon Little H found a counterexample, which made Little X feel upset. To comfort Little X, Little L said: why not see in how many cases your conclusion is true?
Little X cheered up, and everyone threw themselves into the heavy work. They need to count how many triples with satisfy Little X’s transitivity conclusion.
Input Format
The first line contains an integer , the number of nodes in the unrooted tree.
The next lines each contain two integers , where . Here is the index of the parent node of . If , then this node is the root. is the number written on node .
Output Format
Output one integer in one line, the answer.
3
0 3
1 5
1 7
0
5
0 8626
1 29255
2 21486
2 26193
1 22439
7
Hint
Constraints
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号