#P9655. 『GROI-R2』 Beside You
『GROI-R2』 Beside You
Background
Memory Forest.
The mystery of the beginning, someday.
Tell it to the unknown end of this.
— Eguchi Takahiro, “Beside You”.
Problem Description
I believe that everyone who has read this part of the story does not want others to suffer the same pain, but Taniel can still only disappear, right?
Taniel finally left behind a rooted tree with root . On each node of the tree, there is a memory fragment. Some fragments represent the beginning of a world’s memory, while other fragments represent the end of a world’s memory.
At this time, a butterfly モリモリあつし flew to the tree. During its flight on the tree, the butterfly passed through some nodes. Alice can be sure that the number of nodes the butterfly passed through is at least , but she forgot the exact number.
Alice found that all nodes the butterfly passed through are connected to each other. When she looked at every simple path (for each connected block with more than node) from the shallowest node in the connected block to any leaf node of the connected block (a node is a leaf node of the connected block if and only if it has no child nodes in the tree, or none of its child nodes in the tree belongs to the connected block), she found that the memories on these paths are complete. Alice considers the memory on a path to be complete if and only if, on this path, there is neither a case where a world’s memory ends before it starts (a fragment indicating the end of a memory appears when there is no unfinished memory in the middle of the path), nor a case where a world’s memory starts but has no end (there are still unfinished memories after the path ends).
Unfortunately, she has already forgotten which nodes the butterfly passed through, so you need to tell her the maximum possible number of nodes the butterfly could have passed through.
Formal statement
Given a rooted tree with nodes rooted at , each node has a value which is either a left parenthesis or a right parenthesis, and nodes are numbered to .
We define a node set to be valid if and only if (i.e. the size of is greater than ) and for all , the simple path from to passes only through nodes in .
We also define as the edge set with the minimum size such that for all , the simple path from to passes only through edges in .
Define the root of a valid node set as the node in with the minimum depth.
Define to be a leaf node of a valid node set if and only if is not the root of , and the number of nodes satisfying is .
Find a valid node set such that on the path from its root to any of its leaves, the node values along the path, concatenated in order, form a valid parenthesis sequence, and maximize .
We define a valid parenthesis sequence by the following rules:
-
The empty string (i.e. a string of length ) is a valid parenthesis sequence.
-
If strings are both valid parenthesis sequences, then the string (i.e. concatenating and in order) is also a valid parenthesis sequence.
-
If string is a valid parenthesis sequence, then the string is a valid parenthesis sequence.
You need to output the maximum that satisfies the requirements.
Input Format
The first line contains a positive integer , the number of nodes in the tree.
The second line contains a string of length . is ( meaning this node has a fragment marking the start of a memory, and is ) meaning this node has a fragment marking the end of a memory.
The next lines each contain two positive integers , indicating that there is an edge between nodes and .
Output Format
Output one line with one integer, the answer.
3
())
1 2
1 3
3
8
()))())(
1 2
1 3
3 4
3 5
3 6
5 7
2 8
5
Hint
Sample explanation

The maximum valid node set that the butterfly can pass through is .

The maximum valid node set that the butterfly can pass through is .
Constraints
This problem uses bundled testdata.
| Special property | Score | ||
|---|---|---|---|
Special property : it is guaranteed that the tree degenerates into a chain (not necessarily with node as an endpoint).
Special property : it is guaranteed that on any path from any node to a leaf in the original tree, the number of right parentheses is not less than the number of left parentheses.
For all data, , , and is either ( or ).
Translated by ChatGPT 5