PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000008. Đếm con cháu ở độ sâu chỉ định (Count Descendants)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm con cháu ở độ sâu chỉ định (Count Descendants)
Count Descendants
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A rooted tree has vertices with root . The root has depth , and every other vertex has its parent's depth plus .
For each query , count vertices in the subtree rooted at (including ) whose depth measured from root is exactly .
The value is not the distance from to .
Input
- The first line contains .
- The second line contains parent indices .
- The next line contains .
- Each of the next lines contains .
Output
Print one count per query, each on a separate line.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
7
1 1 2 2 4 2
4
1 2
7 2
4 1
5 5
Output
3
1
0
0
Explanation
In the example, there are three depth- descendants of vertex . The subtree of vertex contains itself at depth . The last two queries return .