#P5901. [IOI 2009] Regions

[IOI 2009] Regions

Description

The United Nations Regional Development Agency (UNRDA) has a well-organized structure. It employs NN commissioners, and each commissioner belongs to one of several regions. The commissioners are numbered from 11 to NN according to seniority, where commissioner 11 is the chairperson and has the highest seniority. The regions are numbered from 11 to RR. Every commissioner except the chairperson has a direct mentor. Any direct mentor has higher seniority than the commissioner they mentor.

We say that commissioner AA is a mentor of commissioner BB if and only if AA is the direct mentor of BB, or AA is a mentor of the direct mentor of BB. 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 r1r_1 and r2r_2, the system should answer how many pairs of commissioners e1e_1 and e2e_2 satisfy: e1e_1 belongs to r1r_1, e2e_2 belongs to r2r_2, and e1e_1 is a mentor of e2e_2. Each query has two parameters r1r_1 and r2r_2, and the result is an integer: the number of ordered pairs (e1,e2)(e_1, e_2) 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 N,R,QN, R, Q, separated by a single space, representing the number of employees, the number of regions, and the number of queries.

The next NN lines give the description of the NN commissioners in order of seniority. Line kk describes commissioner number kk. The first line (the one describing the chairperson) contains one integer: the region H1H_1 to which the chairperson belongs. Each of the remaining N1N - 1 lines contains two integers separated by a single space, representing the direct mentor SkS_k of commissioner kk and the region HkH_k to which commissioner kk 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 QQ 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 r1,r2r_1, r_2.

Output Format

For each query, output one line to standard output containing one integer: the number of pairs of commissioners e1e_1 and e2e_2 in UNRDA that satisfy the following conditions: e1e_1 belongs to region r1r_1, e2e_2 belongs to region r2r_2, and e1e_1 is a mentor of e2e_2.

Note: The input guarantees that the correct answer for any query is less than 10910 ^ 9.

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 30%30\% of the testdata, N500N \leq 500.
  • For 55%55\% of the testdata, no region contains more than 500500 commissioners.
  • 15%15\% of the testdata satisfies both conditions above, and 70%70\% of the testdata satisfies at least one of the conditions above.
  • For 100%100\% of the testdata, 1N,Q2×1051 \le N, Q \le 2 \times 10^5, 1Hk,r1,r2R2.5×1041 \le H_k, r_1, r_2 \le R \le 2.5 \times 10^4, 1Sk<k1 \le S_k < k.

Translated by ChatGPT 5