#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 nn 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 nn appears on the second line, denoted as SS. The score PP of sequence SS 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 PP, convert it to binary. If the last three bits are 011011, 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 mm 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 viv_i and vjv_j, the obtained sequence is denoted briefly as iji\sim j. Of course, as mentioned above, the sequence iji\sim j 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 i,j,ki,j,k:

  • If iji\sim j is a good sequence and jkj\sim k is a good sequence, then iki\sim k is a good sequence.

  • If iji\sim j is a bad sequence and jkj\sim k is a bad sequence, then iki\sim k 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 (i,j,k)(i,j,k) with i<j<ki<j<k satisfy Little X’s transitivity conclusion.

Input Format

The first line contains an integer mm, the number of nodes in the unrooted tree.

The next mm lines each contain two integers fi,xif_i,x_i, where fi<if_i<i. Here fif_i is the index of the parent node of viv_i. If fi=0f_i=0, then this node is the root. xix_i is the number written on node viv_i.

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 10%10\% of the testdata, 1m51\leq m\leq 5.

For 30%30\% of the testdata, 1m1001\leq m\leq 100.

For 50%50\% of the testdata, 1m1031\leq m\leq 10^3.

For 100%100\% of the testdata, 1m1051\leq m\leq 10^5.

Translated by ChatGPT 5