题目描述
Problem Description
During the JSOI training camp, Little L, Little H, and Little X often play a game called "Number Removal" to relax:
This is a single-player game that only requires a piece of paper and a pen. The player writes down a sequence of $n$ integers randomly on the paper to start the game.
Each turn, the player selects a number from either the left or right end of the sequence, crosses it out, and writes it down on the next line. When all numbers from the original sequence are crossed out, a new sequence of length $n$, denoted as $S$, is formed on the second line. The score $P$ of sequence $S$ is calculated as follows:
$$P=S_1\times 5^0+S_2\times 5^1+\cdots+S_n\times 5^{n-1}$$
After calculating the score $P$, convert it to its binary representation. If the last three digits are $011$, the player wins the game; otherwise, the player loses.
After playing this game many times, Little L, Little H, and Little X discovered an important fact: For some randomly written sequences, it is impossible to win the game, and such sequences are called "Diabolic Sequences," while others are called "Benign Sequences."
Although the game is highly entertaining, one drawback is that it takes a lot of time to write down the random sequence before each game, which has always troubled Little L, Little H, and Little X.
Until one night before the provincial selection, Little L came up with a brilliant idea to solve this problem: They first draw a large undirected tree (with $m$ nodes) on the paper, with each node labeled with an integer. When they want to play the game, they simply choose any two nodes, find the unique path connecting these two nodes, and list the integers on the nodes along this path (including the endpoints) to form a sequence, which they then use to play the game. If the chosen endpoints are nodes $v_i$ and $v_j$, the resulting sequence is denoted as $i\sim j$. As mentioned before, the sequence $i\sim j$ can also be either "Benign" or "Diabolic."
They found that this improvement made the game much more convenient! Moreover, it added some new趣味 to the game. For example, Little X claimed he discovered an important rule: The property of sequences is transitive, i.e., for any distinct $i, j, k$:
- If $i\sim j$ is Benign and $j\sim k$ is Benign, then $i\sim k$ is Benign.
- If $i\sim j$ is Diabolic or $j\sim k$ is Diabolic, then $i\sim k$ is Diabolic.
This conclusion was surprisingly elegant, but Little H quickly found a counterexample, which made Little X feel disheartened. To comfort Little X, Little L said: "Why don't we see how many cases your conclusion holds?"
Little X cheered up, and they all got to work. They needed to find out how many triplets $(i, j, k)$ exist, where $i < j < k$, such that $i, j, k$ satisfy Little X's transitive rule.
Input Format
The first line contains an integer $m$, representing the number of nodes in the undirected tree.
The next $m$ lines each contain two integers $f_i, x_i$, where $f_i < i$. $f_i$ represents the parent node number of node $v_i$, and if $f_i = 0$, the node is the root. $x_i$ represents the integer written on node $v_i$.
Output Format
A single line containing an integer, representing the answer.
Sample Input and Output
Sample Input #1
3
0 3
1 5
1 7
Sample Output #1
0
Sample Input #2
5
0 8626
1 29255
2 21486
2 26193
1 22439
Sample Output #2
7
Notes/Hints
Data Range
For $10\%$ of the data, $1 \leq m \leq 5$.
For $30\%$ of the data, $1 \leq m \leq 100$.
For $50\%$ of the data, $1 \leq m \leq 10^3$.
For $100\%$ of the data, $1 \leq m \leq 10^5$.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分