/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、ビル内の避難シミュレーションを行っています。
このビルには N 個の部屋と M 本の廊下があります。部屋には 1 から N までの番号が付けられており、各廊下は異なる2つの部屋を双方向に結んでいます。同じ2つの部屋を結ぶ廊下は高々1本です。
廊下や部屋に通行人数の上限はなく、同じ廊下を同じターンに複数の避難者が通ることも、同じ部屋に何人の避難者がいることも問題ありません。各避難者の行動は互いに影響しません。
ビル内には K 人の避難者がいます。避難者 i(1 \leq i \leq K)は最初、部屋 S_i にいます。異なる避難者が同じ部屋にいることもあります。
また、ビル内では Q 個の部屋で火災が発生しています。火災が発生している部屋は P_1, P_2, \ldots, P_Q であり、これらはすべて異なります。火災はシミュレーション中ずっと燃え続けますが、他の部屋に延焼することはありません。
部屋 T は非常出口であり、火災は発生していません。すべての避難者はグラフ全体の構造、火災の位置、他の避難者の位置を完全に把握しており、全員が部屋 T に到達できるよう最適な行動をとるものとします。各避難者は同じターン内でもそれぞれ独立に行動(異なる部屋への移動や、留まること)を選択できます。
シミュレーションの進行
シミュレーションはターン制で進行します。一度 脱落 または 避難完了 した避難者は、以降のターンには参加しません。
開始時の処理(第1ターンの前)
シミュレーション開始時に、以下の処理が行われます:
- 初期位置が部屋 T である避難者は、直ちに 避難完了 となります。
- 初期位置が火災の発生している部屋である避難者は、即座に 脱落 し、避難に失敗します。
制約より部屋 T では火災は発生しないため、両方に該当する避難者はいません。
各ターンの処理
その後、各ターンでは以下の処理がこの順に行われます:
- 移動フェーズ: まだ脱落も避難完了もしていないすべての避難者が同時に行動します。各避難者は、現在いる部屋に廊下で直接つながっている隣接部屋のうち1つへ移動するか、現在の部屋に留まるかを選択します。1回の移動フェーズで移動できるのは廊下1本分(隣接する部屋1つ)までです。
- 避難完了チェック: 移動後に部屋 T にいる避難者は 避難完了 となります。
- 脱落チェック: 移動後に火災の発生している部屋にいる避難者は 脱落 します。制約より部屋 T では火災は発生しないため、ステップ2で避難完了した避難者がこのステップで脱落することはありません。
注意: 火災の発生している部屋への移動は禁止されていませんが、移動した場合は脱落チェックにより脱落します。したがって、1回の移動フェーズで移動できるのは隣接部屋1つまでであるため、火災の発生している部屋を「通過」して向こう側に抜けることはできません。
求めるもの
すべての避難者が脱落することなく部屋 T に到達できるかどうかを判定してください。全員が到達可能な場合は、全員が避難完了するまでに必要な最小ターン数を求めてください。
すべての避難者がシミュレーション開始時点で既に避難完了している場合(全員の初期位置が部屋 T である場合)は、必要なターン数は 0 です。
1人でも脱落が避けられない避難者がいる場合は、全員の避難は不可能です。特に、初期位置が火災の発生している部屋の避難者は開始時に脱落するため、そのような避難者がいる場合は必ず不可能となります。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 0 \leq Q \leq N
- 1 \leq T \leq N
- 1 \leq U_i, V_i \leq N \ (1 \leq i \leq M)
- U_i \neq V_i \ (1 \leq i \leq M)
- 同じ2部屋を結ぶ廊下は高々1本である
- 1 \leq S_i \leq N \ (1 \leq i \leq K)
- S_i は重複することがある
- 1 \leq P_j \leq N \ (1 \leq j \leq Q)
- P_j はすべて異なる
- すべての j について P_j \neq T
- グラフは連結とは限らない
- 避難者の初期位置が火災の発生している部屋であることもありうる
- 入力はすべて整数である
入力
N M K Q T U_1 V_1 U_2 V_2 \vdots U_M V_M S_1 S_2 \ldots S_K P_1 P_2 \ldots P_Q
- 1行目には、部屋の数 N、廊下の数 M、避難者の数 K、火災箇所の数 Q、非常出口の部屋番号 T が、スペース区切りで与えられる。
- 続く M 行のそれぞれには、各廊下が結ぶ2つの部屋の番号 U_i, V_i がスペース区切りで与えられる。
- その次の行には、K 人の避難者の初期位置 S_1, S_2, \ldots, S_K がスペース区切りで与えられる。
- 最後の行には、Q 箇所の火災が発生している部屋の番号 P_1, P_2, \ldots, P_Q がスペース区切りで与えられる。Q = 0 の場合、この行は空行として与えられる。
出力
すべての避難者が脱落することなく部屋 T に到達できる場合は、そのために必要な最小ターン数を1行で出力してください。到達できない避難者が1人でもいる場合は -1 を出力してください。
入力例 1
5 4 3 1 5 1 2 2 3 3 5 2 4 1 3 5 4
出力例 1
3
入力例 2
6 5 3 1 6 1 2 2 3 3 4 4 5 5 6 1 3 5 3
出力例 2
-1
入力例 3
12 14 6 2 12 1 2 2 3 3 4 4 12 2 5 5 7 7 8 8 12 3 6 9 10 9 11 11 12 5 9 1 7 1 5 9 11 12 7 6 10
出力例 3
3
入力例 4
20 19 10 4 20 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 20 4 16 7 17 10 18 13 19 1 3 5 7 9 11 13 15 20 1 16 17 18 19
出力例 4
15
入力例 5
1 0 1 0 1 1
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi is conducting an evacuation simulation inside a building.
The building has N rooms and M corridors. The rooms are numbered from 1 to N, and each corridor bidirectionally connects two distinct rooms. There is at most one corridor connecting any pair of rooms.
There is no limit on the number of people that can pass through corridors or be in rooms. Multiple evacuees can use the same corridor in the same turn, and any number of evacuees can be in the same room without issue. Each evacuee's actions do not affect the others.
There are K evacuees in the building. Evacuee i (1 \leq i \leq K) is initially in room S_i. Different evacuees may be in the same room.
Additionally, fires have broken out in Q rooms of the building. The rooms on fire are P_1, P_2, \ldots, P_Q, all of which are distinct. Fires continue to burn throughout the simulation but do not spread to other rooms.
Room T is the emergency exit, and it is not on fire. All evacuees have complete knowledge of the entire graph structure, fire locations, and positions of other evacuees, and they all act optimally so that everyone can reach room T. Each evacuee can independently choose their action (moving to a different room or staying) within the same turn.
Simulation Progression
The simulation proceeds in turns. Once an evacuee has been eliminated or has completed evacuation, they do not participate in subsequent turns.
Initial Processing (Before Turn 1)
At the start of the simulation, the following processing occurs:
- Evacuees whose initial position is room T immediately complete evacuation.
- Evacuees whose initial position is a room on fire are immediately eliminated and fail to evacuate.
Due to the constraints, room T is never on fire, so no evacuee can satisfy both conditions.
Processing Each Turn
After that, the following processing occurs in each turn in this order:
- Movement Phase: All evacuees who have neither been eliminated nor completed evacuation act simultaneously. Each evacuee chooses either to move to one adjacent room directly connected to their current room by a corridor, or to stay in their current room. In a single movement phase, an evacuee can move at most one corridor (to one adjacent room).
- Evacuation Completion Check: Evacuees who are in room T after moving complete evacuation.
- Elimination Check: Evacuees who are in a room on fire after moving are eliminated. Due to the constraints, room T is never on fire, so an evacuee who completed evacuation in step 2 will not be eliminated in this step.
Note: Moving to a room on fire is not prohibited, but doing so results in elimination during the elimination check. Therefore, since an evacuee can move at most one adjacent room per movement phase, it is impossible to "pass through" a room on fire to reach the other side.
Objective
Determine whether all evacuees can reach room T without being eliminated. If everyone can reach it, find the minimum number of turns required for all evacuees to complete evacuation.
If all evacuees have already completed evacuation at the start of the simulation (i.e., everyone's initial position is room T), the required number of turns is 0.
If even one evacuee cannot avoid elimination, then evacuation of everyone is impossible. In particular, if any evacuee's initial position is a room on fire, they are eliminated at the start, so evacuation is necessarily impossible in such cases.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 0 \leq Q \leq N
- 1 \leq T \leq N
- 1 \leq U_i, V_i \leq N \ (1 \leq i \leq M)
- U_i \neq V_i \ (1 \leq i \leq M)
- There is at most one corridor connecting any pair of rooms
- 1 \leq S_i \leq N \ (1 \leq i \leq K)
- S_i may contain duplicates
- 1 \leq P_j \leq N \ (1 \leq j \leq Q)
- All P_j are distinct
- P_j \neq T for all j
- The graph is not necessarily connected
- An evacuee's initial position may be a room on fire
- All input values are integers
Input
N M K Q T U_1 V_1 U_2 V_2 \vdots U_M V_M S_1 S_2 \ldots S_K P_1 P_2 \ldots P_Q
- The first line contains the number of rooms N, the number of corridors M, the number of evacuees K, the number of fire locations Q, and the emergency exit room number T, separated by spaces.
- Each of the following M lines contains the numbers of the two rooms U_i, V_i connected by each corridor, separated by a space.
- The next line contains the initial positions S_1, S_2, \ldots, S_K of the K evacuees, separated by spaces.
- The last line contains the room numbers P_1, P_2, \ldots, P_Q where fires have broken out, separated by spaces. If Q = 0, this line is given as an empty line.
Output
If all evacuees can reach room T without being eliminated, output the minimum number of turns required on a single line. If even one evacuee cannot reach it, output -1.
Sample Input 1
5 4 3 1 5 1 2 2 3 3 5 2 4 1 3 5 4
Sample Output 1
3
Sample Input 2
6 5 3 1 6 1 2 2 3 3 4 4 5 5 6 1 3 5 3
Sample Output 2
-1
Sample Input 3
12 14 6 2 12 1 2 2 3 3 4 4 12 2 5 5 7 7 8 8 12 3 6 9 10 9 11 11 12 5 9 1 7 1 5 9 11 12 7 6 10
Sample Output 3
3
Sample Input 4
20 19 10 4 20 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 20 4 16 7 17 10 18 13 19 1 3 5 7 9 11 13 15 20 1 16 17 18 19
Sample Output 4
15
Sample Input 5
1 0 1 0 1 1
Sample Output 5
0