#P5658. [CSP-S 2019] 括号树
[CSP-S 2019] 括号树
Background
In this problem, a valid bracket sequence is defined as follows:
()is a valid bracket sequence.- If
Ais a valid bracket sequence, then(A)is a valid bracket sequence. - If
AandBare valid bracket sequences, thenABis a valid bracket sequence.
In this problem, the definitions of a substring and distinct substrings are as follows:
- A substring of a string
Sis a string formed by any continuous characters inS. A substring ofScan be represented by the starting position and the ending position , denoted as (, where denotes the length ofS). - Two substrings of
Sare considered different 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 between any two nodes there exists exactly one simple path.
Xiao Q is a curious kid. One day on his way to school, he encountered a tree of size . The nodes of the tree are numbered from , and node is the root. Except for node , every node has a parent node. The parent of node () is node ().
Xiao Q found that each node of the tree has exactly one bracket, either ( or ). Xiao Q defines as follows: take the brackets on the simple path from the root to node , and arrange them into a string in the order the nodes are visited.
Obviously, is a bracket string, but it is not necessarily a valid bracket sequence. Now Xiao Q wants, for all (), to find how many distinct substrings of are valid bracket sequences.
This problem stumped Xiao Q, so he asks you for help. Suppose has distinct substrings that are valid bracket sequences. You only need to tell Xiao Q the XOR sum of all , namely:
$$(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 is the bitwise XOR operation.
Input Format
The first line contains an integer , denoting the size of the tree.
The second line contains a bracket string of length consisting of ( and ). The -th bracket denotes the bracket on node .
The third line contains integers. The -th integer () denotes the parent index of node .
Output Format
Only one line containing one integer, denoting the answer.
5
(()()
1 1 2 2
6
Hint
[Sample 1 Explanation]
The shape of the tree is shown in the figure below:

The string formed by the brackets on the simple path from the root to node 1, arranged in order, is (, and the number of substrings that are valid bracket sequences is .
The string from the root to node 2 is ((, and the number of substrings that are valid bracket sequences is .
The string from the root to node 3 is (), and the number of substrings that are valid bracket sequences is .
The string from the root to node 4 is (((, and the number of substrings that are valid bracket sequences is .
The string from the root to node 5 is ((), and the number of substrings that are valid bracket sequences is .
[Sample 2]
See brackets/brackets2.in and brackets/brackets2.ans under the contestant directory.
[Constraints]
::cute-table{tuack}
| Test Point ID | Special Property | |
|---|---|---|
| ^ | ||
| ^ | None | |
| ^ | None | |
| ^ |
Translated by ChatGPT 5