#SRD0000098. Quan hệ giữa các vùng (Regions)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Quan hệ giữa các vùng (Regions)
Regions
Source: IOI
Version: Phuoc Hung OJ Extended
Problem Statement
An organization has employees numbered from to . Employee is the chair. Each employee has exactly one direct supervisor , so the supervision relations form a tree rooted at employee .
Employee comes from region . Employee is a manager of employee precisely when is a proper ancestor of in the supervision tree: moving from to supervisors reaches after at least one step. An employee is not a manager of themself.
For each query with two different regions , count ordered pairs such that:
- ;
- ;
- is a manager of .
Distinct pairs are counted separately, even if they share a manager or a subordinate.
Input
The first line contains . The next lines describe employees in order: the first contains only ; the line for employee contains . Finally, lines contain two different region IDs .
Output
For every query, print one integer on its own line: the number of qualifying ordered pairs.
Subtasks
- Subtask 1 (15 points): , , ; each region has at most employees.
- Subtask 2 (15 points): , , .
- Subtask 3 (40 points): , ; each region has at most employees.
- Subtask 4 (30 points): , , ; no additional conditions.
Conditions shared by all subtasks: For every group: , , , ; for ; , with . Every query answer is guaranteed to be less than .
Examples
Input
6 3 4
1
1 2
1 3
2 3
2 3
5 1
1 2
1 3
2 3
3 1
Output
1
3
2
1
Explanation
The tree has as the root, as its children, reporting to , and reporting to . Region IDs are . Query counts only : . Query counts : . Query counts : . Query counts : .