CF1676G.White-Black Balanced Subtrees

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree consisting of nn vertices numbered from 11 to nn. The root is vertex 11. There is also a string ss denoting the color of each vertex: if si=Bs_i = \texttt{B}, then vertex ii is black, and if si=Ws_i = \texttt{W}, then vertex ii is white.

A subtree of the tree is called balanced if the number of white vertices equals the number of black vertices. Count the number of balanced subtrees.

A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. In this problem, all trees have root 11.

The tree is specified by an array of parents a2,…,ana_2, \dots, a_n containing n−1n-1 numbers: aia_i is the parent of the vertex with the number ii for all i=2,…,ni = 2, \dots, n. The parent of a vertex uu is a vertex that is the next vertex on a simple path from uu to the root.

The subtree of a vertex uu is the set of all vertices that pass through uu on a simple path to the root. For example, in the picture below, 77 is in the subtree of 33 because the simple path 7→5→3→17 \to 5 \to 3 \to 1 passes through 33. Note that a vertex is included in its subtree, and the subtree of the root is the entire tree.

The picture shows the tree for n=7n=7, a=[1,1,2,3,3,5]a=[1,1,2,3,3,5], and s=WBBWWBWs=\texttt{WBBWWBW}. The subtree at the vertex 33 is balanced.

你被给定一棵包含 nn 个顶点(编号从 11 到 nn)的有根树,根节点为顶点 11。同时给定一个字符串 ss,表示每个顶点的颜色:若 si=Bs_i = \texttt{B},则顶点 ii 为黑色;若 si=Ws_i = \texttt{W},则顶点 ii 为白色。

若一棵子树中白色顶点的数量等于黑色顶点的数量,则称该子树为平衡的。请计算平衡子树的总数。

树是一个无环的连通无向图。有根树是一棵选定某个顶点作为根的树。本题中,所有树的根均为 11。

该树由一个父节点数组 a2,…,ana_2, \dots, a_n(共 n−1n-1 个数)给出:对所有 i=2,…,ni = 2, \dots, n,aia_i 表示编号为 ii 的顶点的父节点。顶点 uu 的父节点是指从 uu 到根节点的简单路径上的下一个顶点。

顶点 uu 的子树是指所有在通往根节点的简单路径上经过 uu 的顶点构成的集合。例如,在下图中,77 属于顶点 33 的子树,因为简单路径 7→5→3→17 \to 5 \to 3 \to 1 经过了 33。注意:每个顶点都属于其自身的子树,且根节点的子树即为整棵树。

图中展示了 n=7n=7、a=[1,1,2,3,3,5]a=[1,1,2,3,3,5]、s=WBBWWBWs=\texttt{WBBWWBW} 对应的树。顶点 33 处的子树是平衡的。

输入格式

The first line of input contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains an integer nn (2≤n≤40002 \le n \le 4000) — the number of vertices in the tree.

The second line of each test case contains n−1n-1 integers a2,…,ana_2, \dots, a_n (1≤ai<i1 \le a_i \lt i) — the parents of the vertices 2,…,n2, \dots, n.

The third line of each test case contains a string ss of length nn consisting of the characters B\texttt{B} and W\texttt{W} — the coloring of the tree.

It is guaranteed that the sum of the values nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤40002 \le n \le 4000)——树中顶点的数量。

每个测试用例的第二行包含 n−1n-1 个整数 a2,…,ana_2, \dots, a_n(1≤ai<i1 \le a_i \lt i)——顶点 2,…,n2, \dots, n 的父节点。

每个测试用例的第三行包含一个长度为 nn 的字符串 ss,由字符 B\texttt{B} 和 W\texttt{W} 组成——树的着色方案。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the number of balanced subtrees.

对于每个测试用例,输出一个整数——平衡子树的数量。

输入输出样例

  • 输入#1

    3
    7
    1 1 2 3 3 5
    WBBWWBW
    2
    1
    BW
    8
    1 2 3 4 5 6 7
    BWBWBWBW

    输出#1

    2
    1
    4

说明/提示

The first test case is pictured in the statement. Only the subtrees at vertices 22 and 33 are balanced.

In the second test case, only the subtree at vertex 11 is balanced.

In the third test case, only the subtrees at vertices 11, 33, 55, and 77 are balanced.

第一个测试用例如题面图示所示。仅有顶点 22 和 33 处的子树是平衡的。

第二个测试用例中,仅有顶点 11 处的子树是平衡的。

第三个测试用例中,仅有顶点 11、33、55 和 77 处的子树是平衡的。

输入解题思路,AI测评打分。不知道怎么写?

首页