問題一覧 > 通常問題

No.2892 Lime and Karin

レベル : / AI推定難易度 : 2200 / 実行時間制限 : 1ケース 8.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 37
作問者 : 👑 seekworser / テスター : 👑 AngrySadEight kemuniku
お気に入りにしたユーザー ProblemId : 11152 / 自分の提出
問題文最終更新日: 2025-01-30 10:44:59
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 446 (順位表) の他の問題:

Note

The TL for this problem is set strictly. Using a fast language is recommended.

Problem Statement

You are given a tree with $N$ vertices, numbered $1$ to $N$. For $i=1,2,\dots,N-1$, the $i$-th edge connects vertices $u_i$ and $v_i$.

Each vertex of the tree has either a lime tree or a karin tree growing on it. You are given a string $S$: if the $i$-th character of $S$ is 1, a lime tree grows on vertex $i$, and if the $i$-th character of $S$ is 0, a karin tree grows on vertex $i$.

Find the number of pairs of vertices $(l, r)$ satisfying $1 \le l \le r \le N$ such that, among the vertices on the simple path from $l$ to $r$ (including both endpoints), the number of lime trees growing on them is greater than the number of karin trees.

What is a simple path? A "simple path" is a sequence of vertices $v_1, v_2, \dots, v_k$ such that $v_i$ and $v_{i+1}$ are directly connected by an edge for $1 \le i \le k-1$, and $v_i$ and $v_j$ are distinct for all $1 \le i < j \le k$. In a tree, a simple path with specified endpoints is uniquely determined.

Input

$N$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$
$S$

Input

  • $1 \le N \le 100000$
  • $1 \le u_i < v_i \le N \quad (1 \le i \le N-1)$
  • $N, u_i, v_i$ are all integers $(1 \le i \le N-1)$
  • $|S| = N$
  • $S$ consists of the characters 0 or 1.

Output

Print the answer in $1$ line.

Sample

Sample 1
Input
5
1 2
1 3
3 4
3 5
11001
Output
7

$(l, r) = (1, 1), (1, 2), (1, 5), (2, 2), (2, 3), (2, 5), (5, 5)$ satisfy the condition.

Sample 2
Input
1
1
Output
1
Sample 3
Input
10
2 8
4 9
1 6
3 7
1 8
5 8
1 10
1 7
7 9
0111110100
Output
16

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。