Submit | All submissions | Best solutions | Back to list |
REGIONS - REGIONS |
The United Nations Regional Development Agency (UNRDA) has a very well defined organizational structure. It employs a total of people, each of them coming from one of geographically distinct regions of the world. The employees are numbered from to inclusive in order of seniority, with employee number , the Chair, being the most senior. The regions are numbered from to 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 is a manager of employee if and only if is 's supervisor or is a manager of '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 and , how many pairs of employees and exist in the agency, such that employee comes from region , employee comes from region , and is a manager of . Every query has two parameters: the regios and ; and its result is a single integer: the number of different pairs and that satisfy the above-mentioned conditions.
Task
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, answers queries as described above.
Constraints
- the number of employees
- the number of regions
- the number of queries your program will have to answer
- the home region of employee (for )
- the supervisor of employee (for )
- the regions inquired about in a given query
Input
Your program must read from standard input the following data:
- The first line contains the integers , and , in order, separated by single spaces.
- The next lines describe the employees of the agency in order of seniority. The th of these lines describes employee number . The first of these lines (i.e., the one describing the Chair) contains a single integer: the home region of the Chair. Each of the other lines contains two integers separated by a single space: employee 's supervisor , and employee 's home region .
- queries follow. Each query is presented on a single line of standard input and consists of two different integers separated by a single space: the two regions and .
Output
lines should be printed to the standard output, containing answers to subsequent queries. The response to each query must be a single line on standard output containing a single integer: the number of pairs of UNRDA employees and , such that 's home region is , 's home region is and is a manager of .
Note: The test data will be such that the correct answer to any query given on standard input will always be less than .
Grading
For a number of tests, worth a total of 30 points, will not exceed 500.
For a number of tests, worth a total of 55 points, no region will have more than 500 employees.
The tests where both of the above conditions hold are worth 15 points.
The tests where at least one of the two conditions holds are worth 70 points.
Example
For the input data:
6 3 4 1 1 2 1 3 2 3 2 3 5 1 1 2 1 3 2 3 3 1
the correct result is:
1 3 2 1
Added by: | sieunhan |
Date: | 2011-07-18 |
Time limit: | 2s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | IOI 2009 - Day 2 |