CCCGuide

Problem S4: Daily Commute

2021 · S4 · Original problem · DMOJ

Problem Description

Toronto has NN subway stations, numbered from 11 to NN. You start at station 11, and every day, you need to reach station NN to get to school.

There are WW one-way walkways running amongst the stations, the iith of which allows you to walk from station AiA_i to a different station BiB_i (1≤Ai,Bi≤N,Ai≠Bi)(1 \le A_i, B_i \le N, A_i \not= B_i) in 1 minute. There may be multiple walkways connecting any given pair of stations.

The subway line follows a certain route through the NN stations, starting at station 11 and visiting each station once. Initially, this route consists of stations S1,S2,...,SNS_1, S_2, ..., S_N, in that order. S1=1S_1 = 1, and S2,…,SNS_{2}, \ldots, S_{N} is a permutation of the integers 2,…,N2, \ldots, N. Only one subway train runs along this route per day, departing from station 1 at 6am in the morning and taking 1 minute to reach each subsequent station. This means that, mm minutes after 6am, the train will be at station Sm+1S_{m+1} (or at station SNS_N if m≥N−1m \ge N-1).

Over a period of DD days, however, the subway line’s route will keep changing. At the start of the iith day, the XiX_ith station and YiY_ith station (2≤Xi,Yi≤N,Xi≠Yi)(2 \le X_i, Y_i \le N, X_i \not= Y_i) in the route will be swapped. Note that, after each such change, the route will still begin at station 11 and will visit all NN stations once each. Changes will carry over to subsequent days – the route will not automatically reset itself back to S1,…,SNS_1,\ldots,S_N.

On each of these DD days, you’d like to determine how quickly you can get to school so you can begin learning things. On the iith day, starting at 6am in the morning (after the iith update to the subway line’s route), you’ll begin your daily trip to station NN. Each minute, you may either ride the subway to its next stop (if you’re currently at the same station as the train and it hasn’t already completed its route), take a walkway from your current station to another one, or wait at your current station. Note that your trip begins at the same time as the train’s route, meaning that you may choose to immediately ride it if you’d like to, and that you may choose to leave and then get back on the train during your trip.

Input Specification

The first line contains three space-separated integers, NN, WW, and DD.

The next WW lines each contain two space-separated integers, AiA_i and BiB_i (1≤i≤W)(1 \le i \le W).

The next line contains the NN space-separated integers, S1,…,SNS_{1}, \ldots, S_{N}, which form the initial permutation of stations.

The next DD lines each contain two space-separated integers, XiX_i and YiY_i (1≤i≤D)(1 \le i \le D).

The following table shows how the available 15 marks are distributed.

2 marks3≤N≤103 \le N \le 100≤W≤100 \le W \leq 101≤D≤101 \le D \le 10
2 marks3≤N≤2003 \le N \leq 2000≤W≤2000 \le W \leq 2001≤D≤2001 \le D \le 200
3 marks3≤N≤20003 \le N \leq 20000≤W≤20000 \le W \leq 20001≤D≤20001 \le D \le 2000
8 marks3≤N≤200 0003 \le N \leq 200\,0000≤W≤200 0000 \le W \leq 200\,0001≤D≤200 0001 \le D \le 200\,000

Output Specification

The output is DD lines, with one integer per line. The iith line is the minimum number of minutes required to reach station NN on the iith day (1≤i≤D1 \le i \le D).

Sample Input

4 3 3
1 2
3 4
4 1
1 4 3 2
3 4
4 2
3 2

Output for Sample Input

1
2
3

Explanation of Output for Sample Input

At the start of the first day, the subway line’s route will be updated to visit stations [1,4,2,3][1, 4, 2, 3], in that order. On that day, you should simply take the subway to station 44, taking 1 minute.

On the second day, the route will become [1,3,2,4][1, 3, 2, 4], and you should take the subway to station 33 (taking 1 minute) and then walk to station 44 (taking 1 more minute).

On the third day, the route will become [1,2,3,4][1, 2, 3, 4]. One choice of optimal trip involves walking to station 2 (taking 1 minute), then boarding the train there and taking it through station 3 and finally to station 4 (taking another 2 minutes).