luogu#P7737. [NOI2021] 庆典

    ID: 7112 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021NOIO2优化广度优先搜索 BFS拓扑排序Tarjan最近公共祖先 LCA虚树

[NOI2021] 庆典

Problem Description

Country C is a prosperous nation. It consists of nn cities and mm directed roads, with cities numbered from 11 to nn. If starting from city xx and traveling along some roads can reach city yy, then we say city xx can reach city yy, denoted as xyx\Rightarrow y. The roads in Country C have a special property: for three cities xx, yy, zz, if xzx\Rightarrow z and yzy\Rightarrow z, then xyx\Rightarrow y or yxy\Rightarrow x 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 qq parade plans. In the ii-th parade, they want to start from city sis_i, pass through some cities, and end at city tit_i, and during the parade, a city may be visited multiple times. To make the parade more fun, each parade will also temporarily build kk (0k20 \le k \le 2) 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 n,m,q,kn,m,q,k, 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 mm lines each contain two integers u,vu,v, indicating a directed road uvu\rightarrow v.

The next qq lines describe the queries. Each line starts with two integers si,tis_i,t_i, indicating the start city and end city of the parade; then there are kk pairs of integers a,ba,b, where each pair indicates a temporarily added directed road aba\rightarrow b.

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 00.

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 11-st plan, the start city is 11 and the end city is 44. The temporarily built road is 515\rightarrow1. The cities that may finally be passed through are {1,2,4,5}\{1,2,4,5\}.

In the 22-nd plan, the start city is 22 and the end city is 33. The temporarily built road is 535\rightarrow3. The cities that may finally be passed through are {2,3,4,5}\{2,3,4,5\}.

In the 33-rd plan, the start city is 11 and the end city is 22. The temporarily built road is 525\rightarrow2. The cities that may finally be passed through are {1,2,4,5}\{1,2,4,5\}.

In the 44-th plan, the start city is 33 and the end city is 44. The temporarily built road is 515\rightarrow1. Finally, it is impossible to reach city 44 starting from city 33.

Sample #2

See the attachments celebration/celebration2.in and celebration/celebration2.ans.

This sample has the same constraints as test points 575 \sim 7.

Sample #3

See the attachments celebration/celebration3.in and celebration/celebration3.ans.

This sample has the same constraints as test points 101110 \sim 11.

Sample #4

See the attachments celebration/celebration4.in and celebration/celebration4.ans.

This sample has the same constraints as test points 151615 \sim 16.

Sample #5

See the attachments celebration/celebration5.in and celebration/celebration5.ans.

This sample has the same constraints as test points 202520 \sim 25.

Constraints

For all test points, 1n,q3×1051 \le n,q \le 3 \times {10}^5, n1m6×105n - 1 \le m \le 6 \times {10}^5, 0k20 \le k \le 2.

Test Point ID n,qn, q \le kk Special Property
141 \sim 4 55 =0= 0 None
575 \sim 7 10001000 2\le 2
898 \sim 9 3×1053 \times {10}^5 =0= 0 m=n1m = n - 1
101110 \sim 11 =1= 1
121412 \sim 14 =2= 2
151615 \sim 16 =0= 0 None
171917 \sim 19 =1= 1
202520 \sim 25 =2= 2

Translated by ChatGPT 5