E - Nearest Black Vertex Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

NN 個の頂点と MM 本の辺からなる、単純(自己ループおよび多重辺を含まない)かつ連結な無向グラフが与えられます。
i=1,2,,Mi = 1, 2, \ldots, M について、ii 番目の辺は頂点 uiu_i と頂点 viv_i を結ぶ無向辺です。

各頂点を白または黒で塗る方法であって、下記の 22 つの条件をともに満たすものが存在するかを判定し、存在する場合はその一例を示してください。

  • 11 個以上の頂点が黒で塗られている。
  • すべての i=1,2,,Ki = 1, 2, \ldots, K について、下記の条件が成り立つ。
    • 頂点 pip_i と「黒で塗られた頂点のうち頂点 pip_i からの距離が最小であるもの」の距離がちょうど did_i である。

ここで、頂点 uu と頂点 vv の距離は、uuvv を結ぶパスの辺の本数としてあり得る最小値として定義されます。

制約

  • 1N20001 \leq N \leq 2000
  • N1Mmin{N(N1)/2,2000}N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
  • 1ui,viN1 \leq u_i, v_i \leq N
  • 0KN0 \leq K \leq N
  • 1p1<p2<<pKN1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
  • 0diN0 \leq d_i \leq N
  • 与えられるグラフは単純かつ連結
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN MM
u1u_1 v1v_1
u2u_2 v2v_2
\vdots
uMu_M vMv_M
KK
p1p_1 d1d_1
p2p_2 d2d_2
\vdots
pKp_K dKd_K

出力

問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しない場合は、No を出力せよ。
存在する場合は、下記の形式の通り、11 行目に Yes と出力し、22 行目に各頂点を塗る方法を表す文字列 SS を出力せよ。 ここで、SS は長さ NN の文字列であり、i=1,2,,Ni = 1, 2, \ldots, N について SSii 文字目は、頂点 ii を黒で塗るとき 11 、白で塗るとき 00 である。

Yes
SS

答えが複数ある場合、どれを出力しても正解とみなされる。


入力例 1Copy

Copy
5 5
1 2
2 3
3 1
3 4
4 5
2
1 0
5 2

出力例 1Copy

Copy
Yes
10100

例えば、頂点 1,31, 3 を黒に、頂点 2,4,52, 4, 5 を白に塗るという方法が、問題文中の条件を満たします。
実際、i=1,2,3,4,5i = 1, 2, 3, 4, 5 について、頂点 ii と「黒で塗られた頂点のうち頂点 ii からの距離が最小であるもの」の距離を AiA_i で表すと、 (A1,A2,A3,A4,A5)=(0,1,0,1,2)(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2) であり、特に、A1=0,A5=2A_1 = 0, A_5 = 2 が成り立ちます。


入力例 2Copy

Copy
5 5
1 2
2 3
3 1
3 4
4 5
5
1 1
2 1
3 1
4 1
5 1

出力例 2Copy

Copy
No

問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しないため、No を出力します。


入力例 3Copy

Copy
1 0
0

出力例 3Copy

Copy
Yes
1

Score : 500500 points

Problem Statement

You are given a simple connected undirected graph with NN vertices and MM edges (a simple graph contains no self-loop and no multi-edges).
For i=1,2,,Mi = 1, 2, \ldots, M, the ii-th edge connects vertex uiu_i and vertex viv_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,,Ki = 1, 2, \ldots, K, the following holds:
    • the minimum distance between vertex pip_i and a vertex painted black is exactly did_i.

Here, the distance between vertex uu and vertex vv is the minimum number of edges in a path connecting uu and vv.

Constraints

  • 1N20001 \leq N \leq 2000
  • N1Mmin{N(N1)/2,2000}N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
  • 1ui,viN1 \leq u_i, v_i \leq N
  • 0KN0 \leq K \leq N
  • 1p1<p2<<pKN1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
  • 0diN0 \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:

NN MM
u1u_1 v1v_1
u2u_2 v2v_2
\vdots
uMu_M vMv_M
KK
p1p_1 d1d_1
p2p_2 d2d_2
\vdots
pKp_K dKd_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 SS representing a coloring of the vertices in the second line, as shown below.
Here, SS is a string of length NN such that, for each i=1,2,,Ni = 1, 2, \ldots, N, the ii-th character of SS is 11 if vertex ii is painted black and 00 if white.

Yes
SS

If multiple solutions exist, you may print any of them.


Sample Input 1Copy

Copy
5 5
1 2
2 3
3 1
3 4
4 5
2
1 0
5 2

Sample Output 1Copy

Copy
Yes
10100

One way to satisfy the conditions is to paint vertices 1,31, 3 black and vertices 2,4,52, 4, 5 white.
Indeed, for each i=1,2,3,4,5i = 1, 2, 3, 4, 5, let AiA_i denote the minimum distance between vertex ii and a vertex painted black, and we have (A1,A2,A3,A4,A5)=(0,1,0,1,2)(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2), where A1=0,A5=2A_1 = 0, A_5 = 2.


Sample Input 2Copy

Copy
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 2Copy

Copy
No

There is no way to satisfy the conditions by painting each vertex black or white, so you should print No.


Sample Input 3Copy

Copy
1 0
0

Sample Output 3Copy

Copy
Yes
1


2025-04-21 (Mon)
06:11:49 +00:00