luogu#P7737. [NOI2021] 庆典
[NOI2021] 庆典
Problem Description
Country C is a prosperous nation. It consists of cities and directed roads, with cities numbered from to . If starting from city and traveling along some roads can reach city , then we say city can reach city , denoted as . The roads in Country C have a special property: for three cities , , , if and , then or holds.
In one month, it will be the millennium anniversary of the founding of Country C, so its people are preparing a grand parade celebration. Country C has learned that there will be parade plans. In the -th parade, they want to start from city , pass through some cities, and end at city , and during the parade, a city may be visited multiple times. To make the parade more fun, each parade will also temporarily build () directed roads that are only for this parade, meaning other parade plans cannot use the roads built for this parade.
Now Country C wants to know, for each parade plan, how many cities it may possibly pass through.
Note: The temporarily built roads may not satisfy the original special property of Country C's roads.
Input Format
The first line contains four integers , representing the number of cities, the number of roads, the number of parade plans, and the number of temporarily built roads for each parade.
The next lines each contain two integers , indicating a directed road .
The next lines describe the queries. Each line starts with two integers , indicating the start city and end city of the parade; then there are pairs of integers , where each pair indicates a temporarily added directed road .
The testdata guarantees that if the original directed roads of Country C are viewed as undirected edges, then all cities are mutually reachable.
Output Format
For each query, output one line with one integer as the answer. If in a parade it is impossible to reach the end city from the start city, output .
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
4
4
4
0
Hint
Sample Explanation #1
In the -st plan, the start city is and the end city is . The temporarily built road is . The cities that may finally be passed through are .
In the -nd plan, the start city is and the end city is . The temporarily built road is . The cities that may finally be passed through are .
In the -rd plan, the start city is and the end city is . The temporarily built road is . The cities that may finally be passed through are .
In the -th plan, the start city is and the end city is . The temporarily built road is . Finally, it is impossible to reach city starting from city .
Sample #2
See the attachments celebration/celebration2.in and celebration/celebration2.ans.
This sample has the same constraints as test points .
Sample #3
See the attachments celebration/celebration3.in and celebration/celebration3.ans.
This sample has the same constraints as test points .
Sample #4
See the attachments celebration/celebration4.in and celebration/celebration4.ans.
This sample has the same constraints as test points .
Sample #5
See the attachments celebration/celebration5.in and celebration/celebration5.ans.
This sample has the same constraints as test points .
Constraints
For all test points, , , .
| Test Point ID | Special Property | ||
|---|---|---|---|
| None | |||
| None | |||
Translated by ChatGPT 5