#P7500. Metro Passenger Flow
Metro Passenger Flow
Metro Passenger Flow
In Tianqiong City, the metro system consists of lines and stations. Every station is served by at least one line, and each metro line has a list of distinct stations. Two adjacent stations in a line are connected by an undirected edge. Note that lines may intersect at a station, so a station can be served by multiple lines.
There are passengers. The -th passenger wants to travel from station to station (it is guaranteed that ). If the two stations lie on the same metro line, the passenger does not need to transfer. Otherwise, they take several transfers. Regardless of the chosen path, the passenger flow contributed by one journey is determined by the minimum number of transfers.
The passenger flow is defined as follows:
[ \text{Passenger Flow} = \text{Entry Flow} + \text{Transfer Flow} ]
Here, the entry flow is (i.e. the passenger count) and the transfer flow is the number of transfers. In other words, if the minimum number of transfers from to is , then the journey uses metro lines and contributes a passenger flow of .
Note that if there is no route from to , the passenger will take a bus and the passenger flow is .
Your task is to compute the passenger flow contributed by each passenger according to the rules above.
It is guaranteed that every station is served by at least one line.
inputFormat
The input is given in the following format:
N M P k1 s1,1 s1,2 ... s1,k1 k2 s2,1 s2,2 ... s2,k2 ... kM sM,1 sM,2 ... sM,kM s1 t1 s2 t2 ... sP tP
Where:
- $N$ ($1 \leq N$) is the total number of stations.
- $M$ ($1 \leq M$) is the number of metro lines.
- $P$ ($1 \leq P$) is the number of passengers.
- For each $i$ ($1 \leq i \leq M$), $k_i$ denotes the number of stations on metro line $i$, followed by $k_i$ distinct station numbers.
- Each of the next $P$ lines contains two integers $s_j$ and $t_j$, the start and destination stations for the $j$-th passenger.
outputFormat
Output $P$ lines. The $j$-th line should contain a single integer — the passenger flow for the $j$-th passenger. If a passenger is unable to travel from the start to the destination station by metro, output 0 for that passenger.
sample
4 3 1
2 1 2
2 2 3
2 3 4
1 4
3
</p>