#P10304. [THUWC 2020] 道路修建

[THUWC 2020] 道路修建

Problem Description

Country AA is an ancient nation. There are nn cities in this country, numbered from 11 to nn. City 11 is the capital, and it is also the only city with a port. Imported goods enter through the capital’s port and are then transported to other cities.

To make the transportation of imported goods easier, the king of Country AA plans to build some one-way roads specifically for transporting goods. Soon, the ministers of Country AA proposed a road construction plan: the plan consists of mm one-way roads, and using these roads, one can travel from City 11 to any other city in the country. Very specially, the road network formed by these mm roads has no cycles.

Due to limited finances, the king of Country AA plans to first choose n1n - 1 roads from the mm roads to build, to ensure basic transportation capacity. He uses a method similar to depth-first search to determine the n1n - 1 roads to be built with higher priority:

First, let the set SS contain only City 11, and set the current city uu to be City 11.

Then, choose a road that starts from the current city uu and ends at a city vv that is not in set SS. Mark this road as a priority road to be built, add city vv into set SS, and set the parent of city vv to be the current city uu.

Finally, change the current city uu to city vv. If there are multiple choices for city vv, choose the one with the smallest city index; if there is no selectable city vv, then change the current city uu to the parent of uu.

Repeat the above operations until all cities have been added into set SS, then stop.

It can be seen that the above steps select exactly n1n - 1 priority roads, and these priority roads form a tree structure rooted at the capital. We call these priority roads the tree roads.

As the economy develops, Country AA gradually completes the entire road network consisting of the mm roads. However, the tree roads, since they were built earlier, often need maintenance. The ministers of Country AA proposed qq possible maintenance plans. Each maintenance plan requires maintaining all tree roads between city aa and city bb, and it is guaranteed that city aa is an ancestor of city bb in the tree. Such a maintenance plan will have a large impact on the subtree rooted at city bb. The king of Country AA wants you to evaluate each possible maintenance plan: compute, when all tree roads between city aa and city bb are unusable, how many cities in the subtree rooted at city bb will become unreachable from the capital?

Input Format

The first line contains three integers n,m,qn, m, q, representing the number of cities in Country AA, the number of roads in the construction plan, and the number of maintenance plans.

The next mm lines each contain two integers x,yx, y, indicating a one-way road from xx to yy in the construction plan.

The next qq lines each contain two integers a,ba, b, indicating a maintenance plan.

The input guarantees that:

  1. The given construction plan ensures that every city in Country AA is reachable from the capital, and there are no cycles in the plan.
  2. Between any two cities, there is at most one road, and the endpoints of each road have different city indices.
  3. In each maintenance plan, city aa is guaranteed to be some ancestor of city bb in the tree structure formed by the tree roads.

Output Format

Output qq lines, each containing one integer, indicating for each maintenance plan how many cities in the subtree rooted at city bb will be unreachable from the capital.

3 3 2
1 2
2 3
1 3
1 2
1 3

1
0

Hint

Subtasks

Subtask ID Score Special Constraints
11 2020 n,q1000n, q \leq 1000, m1500m \leq 1500
22 2323 It is guaranteed that in each maintenance plan, city aa is the parent of city bb.
33 1111 m=nm = n
44 1919 It is guaranteed that the maintenance plans satisfy a=1a = 1.
55 2727 None.

For 100%100\% of the data, 1n,q1051 \leq n, q \leq 10^5, 1m1.5×1051 \leq m \leq 1.5 \times 10^5.

Translated by ChatGPT 5