: 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 , which is denoted by . The roads in the country satisfy the following property: for three cities , if and , then or .
Now there will be parades throughout 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 will build one-way roads for the exclusive use of that parade, and other parades may not go through the roads built for the specific parade.
Now we want to know the number of cities that COULD BE visited during a parade. Notice 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 Specification
The first line contains four integers denoting the number of cities, the number of roads, the number of parades, and the number of roads that will be built for each parade.
In the following lines, each line contains two integers denoting there is a one-way road .
In the next lines, each line contains two integers denoting the origin and the destination of the parade. Then on the same line, there will be pairs of integers denoting there exists a temporary one-way road for the exclusive use of the parade.
It is guaranteed if we treat the one-way roads originally in the country as bidirectional, the cities are mutually reachable.
Output Specification
For each query, output an integer in a line denoting the answer. If it is not possible to reach the destination from the origin, output 0.
Sample Input 1
5 6 4 1
1 2
1 3
1 4
2 5
4 5
5 4
1 4 5 1
2 3 5 3
1 2 5 2
3 4 5 1
Sample Output 1
4
4
4
0
Explanation for Sample Output 1
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 cities 1,2,4,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 cities 2,3,4,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 cities 1,2,4,5.
In parade 4, the origin is city 3 and the destination is city 4. The temporary road is . We cannot reach city 4 from city 3.
Additional Samples
Sample inputs and outputs can be found here.
- Sample 2 (
celebration2.in
andcelebration2.ans
) corresponds to cases 5-7. - Sample 3 (
celebration3.in
andcelebration3.ans
) corresponds to cases 10-11. - Sample 4 (
celebration4.in
andcelebration4.ans
) corresponds to cases 15-16. - Sample 5 (
celebration5.in
andcelebration5.ans
) corresponds to cases 20-25.
Constraints
For all test cases, , , .
Test Case | Additional Constraints | ||
---|---|---|---|
1~4 | None. | ||
5~7 | |||
8~9 | |||
10~11 | |||
12~14 | |||
15~16 | None. | ||
17~19 | |||
20~25 |
Comments