IOI '09 P7 - Regions

View as PDF

Points: 20 (partial)
Time limit: 7.0s
Memory limit: 128M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

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 regions and ; and its result is a single integer: the number of different pairs and 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.

Constraints

The number of employees
The number of regions
The home region of employee (for )
The supervisor of employee (for )
The regions inquired about in a given query

Input Specification

• 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 .

Interaction

After reading the input data, your program must start alternately reading queries from standard input and writing query results to standard output. The queries must be answered one at a time; your program must send the response to the query it has already received before it can receive the next query.

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 . 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 .

IMPORTANT NOTE: In order to interact properly with the grader, your program needs to flush standard output after every query response.

For a number of tests, worth a total of 30 points, will not exceed .
For a number of tests, worth a total of 55 points, no region will have more than 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.

Sample Input

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

Sample Output

1
3
2
1