No.1477 Lamps on Graph
タグ : / 解いたユーザー数 186
作問者 :
nok0
/ テスター :
PCTprobability
むかで
Problem Statement
You are given a simple undirected graph with $N$ vertices and $M$ edges, where the vertices are numbered from $1$ to $N$ and the edges are numbered from $1$ to $M$. Edge $i$ connects vertex $u_i$ and vertex $v_i$ bidirectionally.
Each vertex $i$ of this graph has an integer $A_i$ written on it.
Also, a lamp is placed on each vertex. Initially, only the lamps on vertices $B_1,B_2,\dots,B_K$ are on, and all other lamps are off.
You may perform the following operation any number of times, possibly $0$ times.
Here, toggling the state of a lamp means turning it off if it is on, and turning it on if it is off.
Is it possible to turn off all the lamps by the operations? If it is possible, show one procedure that minimizes the number of operations. (For concrete examples of the output format, refer to the Output section and the samples.)
Constraints
- All inputs are integers.
- $1 \le N\le 10^5$
- $0 \le M\le \mathrm{min}(10^5, \frac{N(N-1)}{2})$
- $0 \le A_i \le 10^9$
- $1 \le u_i, v_i \le N $
- The given graph is a simple undirected graph.
- $1 \le K \le N$
- $1 \le B_1 < B_2 < \dots < B_K \le N$
Input
$N\ M$ $A_1\ A_2\ \dots\ A_N$ $u_1\ v_1$ $u_2\ v_2$ $\vdots$ $u_M\ v_M$ $K$ $B_1\ B_2\ \dots\ B_K$
Output
If it is impossible to turn off all the lamps, print -1.
If it is possible, follow the format below.
When the procedure consists of $Z$ operations, print $Z+1$ lines as follows.
Here, $x_i$ is the number of the vertex chosen in the $i$-th operation.
$Z$ $x_1$ $x_2$ $\vdots$ $x_Z$
Sample
Sample 1
Input
3 2 3 1 4 1 2 1 3 2 1 3
Output
1 1
Initially, the lamps on vertex $1$ and vertex $3$ are on.
Consider performing the operation on vertex $1$.
First, the state of the lamp on vertex $1$ is toggled, and it turns off.
The vertices connected to vertex $1$ are vertex $2$ and vertex $3$.
For vertex $2$, since $A_1 = 3,A_2=1$, $A_1 < A_2$ is not satisfied, so the state of the lamp on vertex $2$ is not toggled.
For vertex $3$, since $A_1 = 3,A_3=4$, $A_1 < A_3$ is satisfied, so the state of the lamp on vertex $3$ is toggled, and it turns off.
Therefore, by performing the operation on vertex $1$, all the lamps could be turned off in $1$ operation.
Since it is impossible to turn off all the lamps without performing any operation, this is the minimum number of operations.
Sample 2
Input
3 0 100 200 300 3 1 2 3
Output
3 2 3 1
Note that the graph may not be connected.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。