Celebration
Time limit2sMemory limit1024 MB
For each parade, count the cities that can lie on some path from s to t after adding up to two temporary one-way roads.
- Level
Hard8 of 10
- Topics
- Graph, Topological sort, Tree
- Solved
- No attempts yet
Problem
rpeng: I raised the TL to 2 seconds to avoid fast I/O nonsense. I was able to get AC (very close though) using cin/cout.
There are cities and one-directional roads in a country. The cities are numbered . If it is possible to reach city via roads from city , we say city is reachable from city , denoted . The roads in the country satisfy the following property: for three cities , if and , then or .
There will be parades during the celebration. The -th parade starts in city and ends in city after visiting some other cities. A city may be visited multiple times during a parade. To make the parades more interesting, for each parade we build one-way roads for the exclusive use of that parade. Other parades may not use the roads built for a specific parade.
Now we want to know the number of cities that could be visited during a parade. Note that roads built for the exclusive use of a parade do not have to satisfy the property satisfied by the roads originally in the country.
Input
The first line contains four integers , the number of cities, the number of roads, the number of parades, and the number of roads built for each parade.
The following lines each contain two integers , meaning there is a one-way road .
The next lines each contain two integers , the origin and the destination of the parade. On the same line, there are pairs of integers , meaning there is a temporary one-way road for the exclusive use of that parade.
If the original roads are treated as bidirectional, all cities are mutually reachable.
Output
For each parade, output the answer on its own line. The answer is the number of cities that could be visited. If the destination cannot be reached from the origin, output 0.
Constraints
For all test cases, , , and .
Hints
In parade 1, the origin is city 1 and the destination is city 4. The temporary road is . The cities that may be visited are 1, 2, 4, and 5.
In parade 2, the origin is city 2 and the destination is city 3. The temporary road is . The cities that may be visited are 2, 3, 4, and 5.
In parade 3, the origin is city 1 and the destination is city 2. The temporary road is . The cities that may be visited are 1, 2, 4, and 5.
In parade 4, the origin is city 3 and the destination is city 4. The temporary road is . City 4 cannot be reached from city 3.