Regions
The United Nations Regional Development Agency (UNRDA) has a very well defined organizational structure. It employs a total of n people, each of them coming from one of r geographically distinct regions of the world. The employees are numbered from 1 to n inclusive in order of seniority, with employee number 1, the Chair, being the most senior. The regions are
numbered from 1 to r inclusive in no particular order. Every employee except for the Chair has a single supervisor. A supervisor is always more senior than the employees he or she supervises.We say that an employee a is a manager of employee b if and only if a is b’s supervisor or a is a manager of b’s supervisor. Thus, for example, the Chair is a manager of every other employee. Also, clearly no two employees can be each other’s managers.Unfortunately, the United Nations Bureau of Investigations (UNBI) recently received a number of complaints that the UNRDA has an imbalanced organizational structure that favors some regions of the world more than others. In order to investigate the accusations, the UNBI would like to build a computer system that would be given the supervision structure of the UNRDA and would then be able to answer queries of the form: given two different regions r1 and r2, how many pairs of employees e1 and e2 exist in the agency, such that employee e1 comes from region r1,employee e2 comes from region r2, and e1 is a manager of e2. Every query has two parameters:the regions r1 and r2; and its result is a single integer: the number of different pairs e1 and e2 that satisfy the above-mentioned conditions.Write a program that, given the home regions of all of the agency’s employees, as well as data on who is supervised by whom, interactively answers queries as described above.
1 ≤ R ≤ 25,000 The number of regions
1 ≤ Q ≤ 200,000 The number of queries your program will have to answer
1 ≤ Hk ≤ R The home region of employee k (for 1 ≤ k ≤ N)
1 ≤ Sk < k The supervisor of employee k (for 2 ≤ k ≤ N)
1 ≤ r1, r2 ≤ R The regions inquired about in a given query
Your program must read from standard input the following data:
• The first line contains the integers N, R and Q, in order, separated by single spaces.
• The next N lines describe the N employees of the agency in order of seniority. The kth of these N lines describes employee number k. The first of these lines (i.e., the one describing the Chair) contains a single integer: the home region H1 of the Chair. Each of the other N-1 lines contains two integers separated by a single space: employee k’s supervisor Sk, and employee k's home region Hk.
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
Submit solution
Coming laterThe grading service will be connected in a later migration step. You can inspect the task and your previous results now.