102994: [Atcoder]ABC299 E - Nearest Black Vertex
Description
Score : $500$ points
Problem Statement
You are given a simple connected undirected graph with $N$ vertices and $M$ edges (a simple graph contains no self-loop and no multi-edges).
For $i = 1, 2, \ldots, M$, the $i$-th edge connects vertex $u_i$ and vertex $v_i$ bidirectionally.
Determine whether there is a way to paint each vertex black or white to satisfy both of the following conditions, and show one such way if it exists.
- At least one vertex is painted black.
- For every $i = 1, 2, \ldots, K$, the following holds:
- the minimum distance between vertex $p_i$ and a vertex painted black is exactly $d_i$.
Here, the distance between vertex $u$ and vertex $v$ is the minimum number of edges in a path connecting $u$ and $v$.
Constraints
- $1 \leq N \leq 2000$
- $N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace$
- $1 \leq u_i, v_i \leq N$
- $0 \leq K \leq N$
- $1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N$
- $0 \leq d_i \leq N$
- The given graph is simple and connected.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
$N$ $M$ $u_1$ $v_1$ $u_2$ $v_2$ $\vdots$ $u_M$ $v_M$ $K$ $p_1$ $d_1$ $p_2$ $d_2$ $\vdots$ $p_K$ $d_K$
Output
If there is no way to paint each vertex black or white to satisfy the conditions, print No
.
Otherwise, print Yes
in the first line, and a string $S$ representing a coloring of the vertices in the second line, as shown below.
Here, $S$ is a string of length $N$ such that, for each $i = 1, 2, \ldots, N$, the $i$-th character of $S$ is $1$ if vertex $i$ is painted black and $0$ if white.
Yes $S$
If multiple solutions exist, you may print any of them.
Sample Input 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
Sample Output 1
Yes 10100
One way to satisfy the conditions is to paint vertices $1, 3$ black and vertices $2, 4, 5$ white.
Indeed, for each $i = 1, 2, 3, 4, 5$, let $A_i$ denote the minimum distance between vertex $i$ and a vertex painted black, and we have $(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2)$, where $A_1 = 0, A_5 = 2$.
Sample Input 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
Sample Output 2
No
There is no way to satisfy the conditions by painting each vertex black or white, so you should print No
.
Sample Input 3
1 0 0
Sample Output 3
Yes 1
Input
题意翻译
给一个 $N$ 个点 $M$ 条边的无向简单联通图和 $K$ 个限制,你需要将结点染成黑白两色满足所有限制,每个限制形如 $p_i,d_i$ 表示点 $p_i$ 距离最近的黑点的距离恰好等于 $d_i$,问是否存在染色方案,若存在给出任意一组解。 两点之间的距离定义为它们之间的最短路径上的边数。特别的,任何点到其自身的距离为 $0$。 $N\le2000,N-1 \le m \le \min\{n(n-1)/2,2000\},K \le N$Output
对于$i = 1, 2, \ldots, M$,第$i$条边双向连接顶点$u_i$和顶点$v_i$。 确定是否存在一种方法将每个顶点涂成黑色或白色,以满足以下两个条件,并在存在时显示一种这样的方法。
- 至少有一个顶点涂成黑色。
- 对于每个$i = 1, 2, \ldots, K$,以下条件成立:
- 顶点$p_i$和一个涂成黑色的顶点之间的最短距离恰好为$d_i$。
- $1 \leq N \leq 2000$
- $N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace$
- $1 \leq u_i, v_i \leq N$
- $0 \leq K \leq N$
- $1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N$
- $0 \leq d_i \leq N$
- 给定图是简单且连通的。
- 输入中的所有值都是整数。
$N$ $M$ $u_1$ $v_1$ $u_2$ $v_2$ $\vdots$ $u_M$ $v_M$ $K$ $p_1$ $d_1$ $p_2$ $d_2$ $\vdots$ $p_K$ $d_K$部分 部分 输出 如果没有办法将每个顶点涂成黑色或白色以满足条件,则打印
No
。否则,在第一行打印
Yes
,并在第二行打印表示顶点颜色的字符串$S$,如下所示。
这里,$S$是一个长度为$N$的字符串,对于每个$i = 1, 2, \ldots, N$,$S$的第$i$个字符是$1$,如果顶点$i$涂成黑色,则为$0$,如果涂成白色。
Yes $S$如果存在多个解,则可以打印其中任何一个。 部分 部分 样例输入1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2部分 部分 样例输出1
Yes 10100满足条件的一种方法是将顶点$1, 3$涂成黑色,将顶点$2, 4, 5$涂成白色。
实际上,对于每个$i = 1, 2, 3, 4, 5$,让$A_i$表示顶点$i$和涂成黑色的顶点之间的最短距离,我们有$(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2)$,其中$A_1 = 0, A_5 = 2$。 部分 部分 样例输入2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1部分 部分 样例输出2
No没有通过将每个顶点涂成黑色或白色来满足条件的方法,因此你应该打印
No
。
部分
部分
样例输入3
1 0 0部分 部分 样例输出3
Yes 1