An expression is a string consisting only of properly paired brackets. For example, ()()
and (()())
are expressions, whereas )(
and ()(
are not. We can define expressions inductively as follows:
()
is an expression.- If
is an expression, then
is also an expression.
- If
and
are expressions, then
is also an expression.
A tree is a structure consisting of nodes denoted with numbers from
to
and
edges placed so
there is a unique path between every two nodes. Additionally, a single character is written in each node.
The character is either an open bracket
(
or a closed bracket )
. For different nodes and
,
is a
string obtained by traversing the unique path from
to
and, one by one, adding the character written
in the node we're passing through. The string
also contains the character written in node
(at
the first position) and the character written in node
(at the last position).
Find the total number of pairs of different nodes and
such that
is a correct expression.
Input Specification
The first line contains the integer — the number of nodes in the tree. The following line contains
an
-character string where each character is either
)
or (
, the character in the string is the
character written in the node
. Each of the following
lines contains two different positive integers
and
— the labels of nodes directly connected with an edge.
Output Specification
Output the required number of pairs.
Constraints
Subtask | Score | Constraints |
---|---|---|
Sample Input 1
4
(())
1 2
2 3
3 4
Sample Output 1
2
Sample Input 2
5
())((
1 2
2 3
2 4
3 5
Sample Output 2
3
Sample Input 3
7
)()()((
1 2
1 3
1 6
2 4
4 5
5 7
Sample Output 3
6
Comments