F - Black Radius Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 19001900

問題文

NN 頂点の木があります。 頂点は 11 から NN まで番号が振られています。 各 1iN11 ≤ i ≤ N - 1 について、ii 番目の辺は頂点 aia_ibib_i を繋いでいます。 辺の長さはすべて 11 です。

いくつかの頂点はすぬけ君のお気に入りです。 お気に入りの頂点の情報は、長さ NN の文字列 ss として与えられます。 各 1iN1 ≤ i ≤ N について、頂点 ii がお気に入りならば sis_i1 で、頂点 ii がお気に入りでないならば sis_i0 です。

最初、頂点はすべて白色です。 すぬけ君は次の操作をちょうど 11 回だけ行います。

  • お気に入りの頂点 vv をひとつ選び、非負整数 dd をひとつ選ぶ。 頂点 vv から距離 dd 以内の頂点をすべて黒く塗る。

最終的な頂点の色の組合せとして考えられるものは何通りか求めてください。

制約

  • 2N2×1052 ≤ N ≤ 2×10^5
  • 1ai,biN1 ≤ a_i, b_i ≤ N
  • グラフは木である。
  • s=N|s| = N
  • ss01 のみからなる。
  • ss には 1 が含まれる。

部分点

  • 13001300 点分のデータセットでは、ss1 のみからなる。

入力

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

NN
a1a_1 b1b_1
a2a_2 b2b_2
::
aN1a_{N - 1} bN1b_{N - 1}
ss

出力

最終的な頂点の色の組合せとして考えられるものは何通りか出力せよ。


入力例 1Copy

Copy
4
1 2
1 3
1 4
1100

出力例 1Copy

Copy
4

次の 44 通りです。

334d566ec1f4f38d23cd580044f1cd07.png

入力例 2Copy

Copy
5
1 2
1 3
1 4
4 5
11111

出力例 2Copy

Copy
11

このケースは部分点の制約を満たします。


入力例 3Copy

Copy
6
1 2
1 3
1 4
2 5
2 6
100011

出力例 3Copy

Copy
8

Score : 19001900 points

Problem Statement

There is a tree with NN vertices. The vertices are numbered 11 through NN. For each 1iN11 ≤ i ≤ N - 1, the ii-th edge connects vertices aia_i and bib_i. The lengths of all the edges are 11.

Snuke likes some of the vertices. The information on his favorite vertices are given to you as a string ss of length NN. For each 1iN1 ≤ i ≤ N, sis_i is 1 if Snuke likes vertex ii, and 0 if he does not like vertex ii.

Initially, all the vertices are white. Snuke will perform the following operation exactly once:

  • Select a vertex vv that he likes, and a non-negative integer dd. Then, paint all the vertices black whose distances from vv are at most dd.

Find the number of the possible combinations of colors of the vertices after the operation.

Constraints

  • 2N2×1052 ≤ N ≤ 2×10^5
  • 1ai,biN1 ≤ a_i, b_i ≤ N
  • The given graph is a tree.
  • s=N|s| = N
  • ss consists of 0 and 1.
  • ss contains at least one occurrence of 1.

Partial Score

  • In the test set worth 13001300 points, ss consists only of 1.

Input

The input is given from Standard Input in the following format:

NN
a1a_1 b1b_1
a2a_2 b2b_2
::
aN1a_{N - 1} bN1b_{N - 1}
ss

Output

Print the number of the possible combinations of colors of the vertices after the operation.


Sample Input 1Copy

Copy
4
1 2
1 3
1 4
1100

Sample Output 1Copy

Copy
4

The following four combinations of colors of the vertices are possible:

334d566ec1f4f38d23cd580044f1cd07.png

Sample Input 2Copy

Copy
5
1 2
1 3
1 4
4 5
11111

Sample Output 2Copy

Copy
11

This case satisfies the additional constraint for the partial score.


Sample Input 3Copy

Copy
6
1 2
1 3
1 4
2 5
2 6
100011

Sample Output 3Copy

Copy
8


2025-03-15 (Sat)
23:24:29 +00:00