#CSPS2019B. 括号树 (Bracket Tree)
括号树 (Bracket Tree)
Description
In this question, we define a bracket string that meets the following requirements as a "legal bracket string":
()
is a "legal bracket string".- If
A
is a "legal bracket string", then (A) is also a "legal bracket string". - If
A
andB
are both "legal bracket strings", thenAB
is also a "legal bracket string".
The definition of substring and different substring in this question is as follows:
- We use (, which is the length of ) to represent a substring of . It means a string consisting of any consecutive characters from to in .
- Two substrings of S are considered different if and only if their positions in S are different, (i.e. is different or is different).
Little Q has a rooted tree which rooted at node and the number of nodes is . Except for node , the parent node of node is .
Little Q finds that there is exactly one bracket on each node of this tree, which may be (
or )
. He defines as a string composed of brackets on the simple path from the root node to the node and arranged in sequence according to the sequence of the nodes.
Obviously is a bracket string, but it may not be a "legal bracket string". So now Little Q wants to find out how many different substrings of are "legal bracket strings" for all .
Assuming that there are different substrings of as "legal bracket strings", you only need to tell Little Q the bitwise XOR of all , which is:
Among them, is bitwise XOR operation.
Input Format
The first line contains an integer , which represents the number of nodes in the tree.
The second line contains an bracket string of length , which the -th bracket represents the bracket on the -th node.
The third line contains integer. The -th integer represents the father of -th node .
Output Format
Print one integer in a single line, which is the answer.
5
(()()
1 1 2 2
6
The shape of the tree is as follows:
- The brackets on the simple path from the root to node are arranged in order to form a string of
(
, and the number of substrings that are "legal bracket strings" is . - The brackets on the simple path from the root to node are arranged in order to form a string of
((
, and the number of substrings that are "legal bracket strings" is . - The brackets on the simple path from the root to node are arranged in order to form a string of
()
, and the number of substrings that are "legal bracket strings" is . - The brackets on the simple path from the root to node are arranged in order to form a string of
(((
, and the number of substrings that are "legal bracket strings" is . - The brackets on the simple path from the root to node are arranged in order to form a string of
(()
, and the number of substrings that are "legal bracket strings" is .
Sample 2
See attached files brackets2.in and brackets2.ans.
Sample 3
See attached files brackets3.in and brackets3.ans.
Constraints
# | Special Properties | |
---|---|---|
None | ||
None | ||