#P5901. [IOI 2009] Regions
[IOI 2009] Regions
Description
The United Nations Regional Development Agency (UNRDA) has a well-organized structure. It employs commissioners, and each commissioner belongs to one of several regions. The commissioners are numbered from to according to seniority, where commissioner is the chairperson and has the highest seniority. The regions are numbered from to . Every commissioner except the chairperson has a direct mentor. Any direct mentor has higher seniority than the commissioner they mentor.
We say that commissioner is a mentor of commissioner if and only if is the direct mentor of , or is a mentor of the direct mentor of . Clearly, the chairperson is a mentor of every other commissioner, and no two commissioners are mentors of each other.
Now, to investigate many accusations that UNRDA is biased toward certain regions due to an unbalanced organizational structure, UNRDA wants to build a computer system: given the direct mentor relationships between commissioners, the system can answer queries of the following form. Given two regions and , the system should answer how many pairs of commissioners and satisfy: belongs to , belongs to , and is a mentor of . Each query has two parameters and , and the result is an integer: the number of ordered pairs satisfying the conditions above.
Task: Write a program that, given each commissioner’s region and direct mentor, answers the queries above online.
Forced online will be conducted in an interactive format.
Input Format
The first line contains three integers , separated by a single space, representing the number of employees, the number of regions, and the number of queries.
The next lines give the description of the commissioners in order of seniority. Line describes commissioner number . The first line (the one describing the chairperson) contains one integer: the region to which the chairperson belongs. Each of the remaining lines contains two integers separated by a single space, representing the direct mentor of commissioner and the region to which commissioner belongs.
Interactive Format
After reading all input data, your program must then read the queries one by one from standard input, and output the answers to standard output. You must answer exactly queries, one at a time. Before reading the next query, you must answer the current query first.
Each query is one line from standard input, containing two distinct integers .
Output Format
For each query, output one line to standard output containing one integer: the number of pairs of commissioners and in UNRDA that satisfy the following conditions: belongs to region , belongs to region , and is a mentor of .
Note: The input guarantees that the correct answer for any query is less than .
Special note: To interact correctly with the interactive library, your program must flush the standard output buffer after answering each query. You also need to avoid accidentally blocking when reading from standard input, which may happen if you use a statement like scanf("%d\n", ...).
You may use the following statements to flush the buffer:
- For C/C++:
fflush(stdout); - For C++:
std::cout << std::flush; - For Java:
System.out.flush(); - For Python:
stdout.flush(); - For Pascal:
flush(output); - For other languages, please refer to the documentation of the corresponding language.
In particular, for C++, when outputting a newline, if you use std::endl instead of '\n', the buffer can also be flushed automatically.
6 3 4
1
1 2
1 3
2 3
2 3
5 1
1 2
1 3
2 3
3 1
1 [刷新缓冲区]
3 [刷新缓冲区]
2 [刷新缓冲区]
1 [刷新缓冲区]
Hint
Constraints and Notes
- For of the testdata, .
- For of the testdata, no region contains more than commissioners.
- of the testdata satisfies both conditions above, and of the testdata satisfies at least one of the conditions above.
- For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号