#P69. [CSP-S 2019 Day 1] Bracket Tree
[CSP-S 2019 Day 1] Bracket Tree
Person in Charge
Problem Background
The definition of a valid parentheses string in this problem is as follows:
()is a valid parentheses string.- If
Ais a valid parentheses string, then(A)is a valid parentheses string. - If
AandBare valid parentheses strings, thenABis a valid parentheses string.
The definitions of substring and distinct substrings in this problem are as follows:
- A substring of string
Sis a string composed of consecutive arbitrary number of characters inS. A substring ofScan be represented by the starting position and ending position , denoted as (, where represents the length of ). - Two substrings of
Sare considered distinct if and only if their positions inSare different, i.e., is different or is different.
Problem Description
A tree of size contains nodes and edges. Each edge connects two nodes, and there is exactly one simple path between any two nodes that is reachable from each other.
Little Q is a curious child. One day on his way to school, he encountered a tree of size , where the nodes are numbered from to , and node is the root of the tree. Except for node , each node has a parent node: the parent of node () is node ().
Little Q found that each node of the tree has exactly one parenthesis, which can be ( or ). Little Q defines as the string formed by arranging the parentheses on the simple path from the root node to node in the order of the nodes passed.
Obviously, is a parentheses string, but not necessarily a valid one. Therefore, Little Q now wants to find, for each (), the number of distinct substrings of that are valid parentheses strings.
This problem stumped Little Q, so he had to ask for your help. Let be the number of distinct substrings of that are valid parentheses strings. You only need to tell Little Q the XOR sum of all , i.e.:
$$(1 \times k_1)\ \text{xor}\ (2 \times k_2)\ \text{xor}\ (3 \times k_3)\ \text{xor}\ \cdots\ \text{xor}\ (n \times k_n)$$where denotes the bitwise XOR operation.
Input Format
The first line contains an integer , representing the size of the tree.
The second line contains a parentheses string of length composed of ( and ), where the -th parenthesis represents the parenthesis on node .
The third line contains integers, where the -th integer () represents the parent number of node .
Output Format
Output a single integer representing the answer.
Samples
5
(()()
1 1 2 2
6
Sample 1 Explanation
The structure of the tree is shown in the figure below:

- The string formed by arranging the parentheses on the simple path from the root to node is
(, and the number of substrings that are valid parentheses strings is . - The string for the path from the root to node is
((, and the number of valid parentheses substrings is . - The string for the path from the root to node is
(), and the number of valid parentheses substrings is . - The string for the path from the root to node is
(((, and the number of valid parentheses substrings is . - The string for the path from the root to node is
((), and the number of valid parentheses substrings is .
Sample 2
See brackets/brackets2.in and brackets/brackets2.ans in the contestant's directory.
Sample 3
See brackets/brackets3.in and brackets/brackets3.ans in the contestant's directory.
Data Range
| Test Case ID | Special Property | |
|---|---|---|
| None | ||
| None | ||
For of the data: , .