#P7500. 「HMOI R1」地铁客流

「HMOI R1」地铁客流

Description

The metro system of Tiankong City consists of MM lines and has a total of NN stations. Every station has lines stopping at it. On each line, two adjacent stations can be viewed as connected by an undirected edge. Line ii has kik_i stations, and these stations are all distinct.

These lines intersect at some stations, meaning that one station may have many lines stopping at it.

Now, there are PP passengers. Passenger jj wants to travel from station sjs_j to station tjt_j, and it is guaranteed that these two stations are different. If the two stations are not on the same line, the passenger will transfer several times. As a technical staff member of the Tiankong Metro, you need to compute the passenger flow contributed by these passengers.


Note that the method used to compute passenger flow here is different from real-world applications.

Passenger flow == entry flow ++ transfer flow. The entry flow is the number of passengers; the transfer flow is the number of transfers made by passengers. There may be multiple paths between the start and the destination; in that case, the metro passenger flow is independent of which path the passenger chooses. When calculating, we only consider paths with the minimum number of transfers.


Notes:

  • Suppose the minimum number of transfers from ss to tt is trans\rm trans. Then the passenger needs to take trans+1\rm trans + 1 lines, contributing trans+1\rm trans + 1 passenger flow.
  • When the passenger cannot reach the destination from the start (i.e., there is no path between ss and tt), they will take a bus instead, and the passenger flow is counted as 00.

Input Format

The first line contains three integers N,M,PN, M, P.

The next MM lines each describe one metro line:
The first integer in each line is ki (2kiN)k_i\ (2 \le k_i \le N), the number of stations on this line;
Then follow kik_i integers q (1qN)q\ (1 \le q \le N), the station indices that this line passes through in order.

The next PP lines each contain two integers sj,tj (1sj,tjN; sjtj)s_j, t_j\ (1 \le s_j, t_j \le N;\ s_j \neq t_j), describing the start and end stations of one passenger’s trip.

Output Format

Output one integer in a single line, representing the passenger flow contributed by these passengers.

8 4 4
4 1 2 3 5
2 3 6
2 2 4
3 7 4 8
1 6
4 3
4 8
6 7
9
8 4 4
4 1 2 3 5
3 6 3 8
2 2 4
3 7 4 8
1 6
4 3
4 8
6 7
7
8 3 4
4 1 2 3 5
2 3 6
3 7 4 8
1 6
4 3
4 8
6 7
3

Hint

Sample explanation:

  • By default, passengers will choose a path with the minimum number of transfers.
  • The number on an edge indicates the index of the metro line it belongs to.

Sample 1 is shown in the figure:

Passenger 11 will take Line 11 from 11 to 33, then take Line 22 from 33 to 66;
Passenger 22 will take Line 33 from 44 to 22, then take Line 11 from 22 to 33;
Passenger 33 will take Line 44 from 44 to 88;
Passenger 44 will take Line 22 from 66 to 33, take Line 11 from 33 to 22, then take Line 33 from 22 to 44, and finally take Line 44 from 44 to 77.

Thus, the four passengers contribute passenger flow of 2,2,1,42, 2, 1, 4 respectively, and the answer is 99.

Sample 2 is shown in the figure:

Compared with Sample 1,
Passenger 22 can choose another route: take Line 44 from 44 to 88, then take Line 22 from 88 to 33, which also involves one transfer;
Passenger 44 can choose another route with fewer transfers: take Line 44 from 77 to 88, then take Line 22 from 88 to 66, with only one transfer.

The total passenger flow is 2+2+1+2=72 + 2 + 1 + 2 = 7.

Sample 3 is shown in the figure:

Compared with Sample 1, the travel paths of passengers 22 and 44 are not connected, so they are not counted in the passenger flow.

The total passenger flow is 2+0+1+0=32 + 0 + 1 + 0 = 3.


Let the number of lines stopping at station ii be sizi\mathrm{siz}_i.
For all testdata:

  • 2N1052 \le N \le 10^5;
  • 1M10001 \le M \le 1000;
  • 1P1001 \le P \le 100;
  • 1sizi501 \le \mathrm{siz}_i \le 50.

This problem uses bundled tests.

No. Constraints Score
11 N,P5; M3N, P \le 5;\ M \le 3 1010
22 ki=2k_i = 2 2020
33 N,M50; P10N, M \le 50;\ P \le 10
44 M500; P10M \le 500;\ P \le 10
55 No further constraints 3030

Due to the large input size, do not use cin with stream synchronization enabled.

You can disable synchronization for cin by using std::ios::sync_with_stdio(false).

You may also use the following fast input template, which supports reading non-negative integers within the int range.

int readInt() {
	int ret = 0; char o;
	while (!isdigit(o = getchar()));
	do ret = ret * 10 + (o ^ 48);
	while (isdigit(o = getchar()));
	return ret;
}

  • Idea: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
  • Solution: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
  • Code: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
  • Data: Nanqiao Bus Station (Nánqiáo qìchēzhàn).

Translated by ChatGPT 5