#abc299e. E - Nearest Black Vertex
E - Nearest Black Vertex
Score : points
问题描述
你得到一个包含 个顶点和 条边的简单连通无向图(简单图不含自环且不含多重边)。对于 ,第 条边在顶点 和顶点 之间双向连接。
确定是否存在一种方法为每个顶点涂上黑色或白色以满足以下两个条件,并在存在这样的方法时展示其中一种。
- 至少有一个顶点被涂成黑色。
- 对于所有 ,满足以下条件:
- 顶点 到任意一个被涂成黑色顶点的最短距离恰好为 。
这里,顶点 和顶点 之间的距离是在一条连接 和 的路径中所需的最小边数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a simple connected undirected graph with vertices and edges (a simple graph contains no self-loop and no multi-edges).
For , the -th edge connects vertex and vertex 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 , the following holds:
- the minimum distance between vertex and a vertex painted black is exactly .
Here, the distance between vertex and vertex is the minimum number of edges in a path connecting and .
Constraints
- 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:
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 representing a coloring of the vertices in the second line, as shown below.
Here, is a string of length such that, for each , the -th character of is if vertex is painted black and if white.
Yes
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 black and vertices white.
Indeed, for each , let denote the minimum distance between vertex and a vertex painted black, and we have , where .
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
update @ 2024/3/10 12:24:50