#P9026. [CCC 2021 S4] Daily Commute

[CCC 2021 S4] Daily Commute

Problem Description

There are NN subway stations. Your home is at 11, and your school is at NN.

There are WW one-way walking paths. Each path takes one minute to travel.

In addition, there is a circular subway line that visits S1,S2,,SNS_1,S_2,\cdots,S_N in order, and it is guaranteed that S1=1S_1=1. Every day, there is exactly one subway train that departs from S1S_1 at time 00, and it arrives at SiS_i at exactly minute ii.

Over the next DD days:

  • Swap SXiS_{X_i} and SYiS_{Y_i}. Note that the change is permanent.
  • Query the shortest time from 11 to NN. When you depart, the subway is at 11.

Input Format

The first line: N,W,DN,W,D.

The next WW lines: Ai,BiA_i,B_i represent a one-way walking path.

The next line contains NN numbers: SS.

The next DD lines: Xi,YiX_i,Y_i, and it is guaranteed that 2Xi,YiN,XiYi2\leq X_i,Y_i\leq N,X_i\neq Y_i.

Output Format

Output DD lines, the answer for each day.

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

1
2
3

Hint

$$3\leq N\leq 200000,0\leq W\leq 200000,1\leq D\leq 200000$$

Translated from CCC2021 S4

Please pay attention to constants.

Translated by ChatGPT 5