P8287 "DAOI R1" Flame
Background
> Tasting the apples in heaven is nothing special. I want to taste the apples in hell.
Description
In the darkness there are black flames, and only those with sharp eyes can catch them.
With this tiny bit of humble light, walk deep into hell...
Welcome to the judgment ground of hell.
$ \texttt{hhpq.} $ has forced D into the judgment ground of hell. D must escape before a Flame Prison is successfully formed in the City of Karma Fire, so he wants to know how many seconds are left.
In this City of Karma Fire, there are $n$ altars and $m$ bidirectional roads along which flames can spread.
It is known that there are $k$ altars whose flames (fire seeds) have already been lit. Starting from the first second, the fire will begin to spread from the lit altars to altars that can be reached and are not yet lit.
Once an altar is lit, it is activated instantly, and it will connect a holy wall of karma fire to every altar that has a road to it.
When there exists a closed shape formed by these holy walls of karma fire, the Flame Prison is formed successfully.
### Simplified statement
You are given an undirected graph with $n$ vertices and $m$ edges. Each vertex has a label. Initially, $k$ vertices have label `1` (their indices are given), and the remaining vertices have label `0`.
Each second, for every vertex labeled `1`, all vertices **connected to it by an edge** will have their labels changed to `1`.
Find the minimum number of seconds needed so that the vertices labeled `1` together with the edges between adjacent `1`-labeled vertices can form a simple cycle.
**In other words, find the minimum number of seconds after which there exists a set of vertices labeled `1` that forms a simple cycle.**
Input Format
The first line contains three positive integers: $n, m, k$.
The next $m$ lines each contain two positive integers: $x, y$ (altar $x$ and altar $y$ are connected by a karma-fire road).
The last line contains $k$ positive integers: the indices of the altars that have already been lit (have fire seeds).
Output Format
If D can escape, output how much time D has left.
Otherwise, if it is impossible to form a Flame Prison, output ``` Poor D! ```.
Explanation/Hint
### Sample explanation
#### Sample 1 explanation
When time reaches the first second, the flame at altar $1$ spreads to altars $2$ and $3$. At this point, a closed shape has already been formed, so the answer is $1$.
#### Sample 2 explanation
It can be proved that no simple cycle can be formed in this case.
### Constraints
| Subtask | $n\leq$ | $m\leq$ | $k\leq$ | Points |
| :----------: | :----------: | :----------: | :----------: | :----------: |
| $0$ | $10^6$ | $n-1$ | $10^4$ | $5$ |
| $1$ | $10^6$ | $2\times10^6$ | $1$ | $10$ |
| $2$ | $10$ | $45$ | $1$ | $5$ |
| $3$ | $200$ | $500$ | $10$ | $10$ |
| $4$ | $2\times 10^3$ | $5\times 10^3$ | $50$ | $10$ |
| $5$ | $5\times10^5$ | $6\times10^5$ | $500$ | $30$ |
| $6$ | $10^6$ | $2\times10^6$ | $10^4$ | $30$ |
### Guarantees and notes
It is guaranteed that there are no multiple edges or self-loops in the graph.
It is guaranteed that the given graph is a connected undirected graph.
### Help
The input size is large, so a fast input method is recommended.
Translated by ChatGPT 5