luogu#P5384. [Cnoi2019] 雪松果树
[Cnoi2019] 雪松果树
Background
Gensokyo, winter.
Once a year, the cedar cone trees growing on the high mountains bear fruit again.
Cirno somehow got cedar cones, then happily ate of them, and in the end only cone was left.
Cirno felt sad because she would not be able to eat cedar cones in the future, so she decided to plant it by the beautiful Misty Lake.
On the first day, it sprouted.
On the second day, the cedar cone tree grew into a towering tree, covered with cedar cones.
Cirno had some questions she wanted to know before the cedar cones ripened, but now she was busy collecting cedar cones, so she threw the questions to you.
Problem Description
The cedar cone tree is a tree with nodes rooted at .
In addition, Cirno has queries. Each query is an ordered pair , asking how many -cousins node has.
We define:
The 1-father of node is the node on the path (excluding ) that is closest to .
The -father of node is the 1-father of the node “the -father of ”.
The -son of node is the set of all nodes whose -father is .
The -cousin of node is the -son of the node “the -father of ” (excluding itself).
Input Format
The first line contains two integers and .
The second line contains integers. The -th integer denotes the 1-father of node .
The following lines each contain an ordered pair .
Output Format
Output one line with numbers, where each number is the answer to a query. If does not have a -father, output .
5 2
1 2 1 4
2 1
3 2
1 1
Hint
Constraints: |Test Point ID|||Special Property| |----|----|----|-----| |1,2|||| |3,4|||| |5,6|||| |7|||| |8,9,10|||| |11,12,13,14|||The tree is generated randomly| |15,16,17,18,19,20||||
In addition, there is a set of hack testdata worth points.
Translated by ChatGPT 5