#P5384. [Cnoi2019] 雪松果树

    ID: 3862 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2019O2优化深度优先搜索,DFS差分

[Cnoi2019] 雪松果树

Description

The cedar cone tree is a tree with NN nodes rooted at 11.

In addition, Cirno has QQ queries. Each query is an ordered pair (u,k)(u,k), asking how many kk-cousins node uu has.

We define:

The 1-father of node uu is the node on the path (1,u)(1, u) (excluding uu) that is closest to uu.

The kk-father of node uu is the 1-father of the node “the (k1)(k-1)-father of uu”.

The kk-son of node uu is the set of all nodes whose kk-father is uu.

The kk-cousin of node uu is the kk-son of the node “the kk-father of uu” (excluding uu itself).

Input Format

The first line contains two integers NN and QQ.

The second line contains N1N-1 integers. The ii-th integer denotes the 1-father of node i+1i+1.

The following QQ lines each contain an ordered pair (u,k)(u,k).

Output Format

Output one line with QQ numbers, where each number is the answer to a query. If uu does not have a kk-father, output 00.

5 2
1 2 1 4
2 1
3 2
1 1

Hint

Constraints: |Test Point ID|NN\le|QQ\le|Special Property| |----|----|----|-----| |1,2|100100|100100|| |3,4|100100|10610^6|| |5,6|10510^5|100100|| |7|10410^4|50005000|| |8,9,10|10510^5|10510^5|| |11,12,13,14|10610^6|10610^6|The tree is generated randomly| |15,16,17,18,19,20|10610^6|10610^6||

In addition, there is a set of hack testdata worth 2020 points.

Translated by ChatGPT 5