#P3920. [WC2014] 紫荆花之恋
[WC2014] 紫荆花之恋
Description
Qiangqiang and Mengmeng are good friends. One day while strolling outside, they suddenly saw a bauhinia tree ahead. It was the season when bauhinia blossoms flutter, and countless petals were growing from the tree at a speed visible to the naked eye.
Looking closely, this big tree is actually a weighted tree. At each moment it grows a new leaf node. There is a cute little spirit on every node, and a new spirit also appears on each newly grown node. Spirits are adorable but fragile creatures. Each spirit has a sensitivity value . Spirits and become friends if and only if their distance on the tree satisfies , where denotes the sum of edge weights along the unique path from to on this tree.
Qiangqiang and Mengmeng are curious: after each new leaf node appears, how many pairs of friends are there in total on the tree?
We assume the tree is initially empty, and nodes are numbered from in the order they are added. Since Qiangqiang is very curious, you must output the total number of friend pairs immediately after each new node appears, without delay.
Input Format
The first line contains an integer representing the test point ID.
The second line contains a positive integer , the total number of nodes to be added.
Let be the total number of friend pairs before adding the current node, with initial value .
In each of the next lines, the -th line contains three non-negative integers , meaning that the parent ID of node is (where denotes XOR; the data guarantees that the result after this operation lies between and inclusive), the edge weight to its parent is , and the sensitivity of the spirit on node is .
Note that , meaning node is the root. For , the parent ID is at least .
Output Format
Output lines. The -th line contains one integer: the number of pairs of friends in the tree after adding node .
0
5
0 0 6
1 2 4
0 9 4
0 5 5
0 2 4
0
1
2
4
7
Hint
All data satisfy , , and .
| Test point ID | Assumption |
|---|---|
| , node has at most two children, other nodes have at most one child | |
| , | |
| , the tree is generated randomly | |
Translated by ChatGPT 5
京公网安备 11011102002149号