Zagrade | 프로그래밍의 벗 PivotOJ
PivotOJ

Zagrade

시간 제한: 3000ms메모리 제한: 1024MB출처: CHC 2017 Croatian Olympiad in InformaticsBOJ 14521

문제

An expression is a string of 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 aa is an expression, then “(aa)” is also an expression.
  • If aa and bb are expressions, then “abab” is also an expression.

A tree is a structure consisting of nn nodes denoted with numbers from 11 to nn and n1n - 1 edges placed so there is a unique path between each 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 aa and bb, wa,bw_{a,b} is a string obtained by traversing the unique path from aa to bb and, one by one, adding the character written in the node we’re passing through. The string wa,bw_{a,b} also contains the character written in the node aa (at the first position) and the character written in the node bb (at the last position).

Find the total number of pairs of different nodes aa and bb such that wa,bw_{a,b} is a correct expression.

입력

The first line of contains the an integer nn — the number of nodes in the tree. The following line contains an nn-character string where each character is either “)” or “(”, the jjth character in the string is the character written in the node jj. Each of the following n1n - 1 lines contains two different positive integers xx and yy (1 ≤ x, y ≤ n) — the labels of nodes directly connected with an edge.

출력

Output the required number of pairs.

예제

예제 1

입력
4
(())
1 2
2 3
3 4
출력
2

예제 2

입력
5
())((
1 2
2 3
2 4
3 5
출력
3

예제 3

입력
7
)()()((
1 2
1 3
1 6
2 4
4 5
5 7
출력
6
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.