Given a tree with '(' or ')' on each node, count ordered pairs (a,b) whose path string w_{a,b} is a properly matched bracket expression.
Hard9TreeDivide and conquerPrefix sumDFSNo attempts yetTime limit3sMemory limit1024 MBAn expression is a string consisting only of properly paired brackets. For example, "()()" and "(()())" are expressions, whereas ")(" and "()(" are not. Expressions can be defined inductively as follows:
()" is an expression.(a)" is also an expression.A tree is a structure of n nodes numbered 1 to n and n−1 edges, placed so that there is exactly one path between any two nodes. A single character is written in each node, and that character is either an open bracket "(" or a closed bracket ")". For different nodes a and b, wa,b is the string obtained by walking the unique path from a to b and appending, one by one, the character written in each node you pass through. The string wa,b also contains the character of node a (at the first position) and the character of node b (at the last position).
Find the number of ordered pairs (a,b) of different nodes such that wa,b is a correct expression. The string wb,a is the reverse of wa,b, so (a,b) and (b,a) are counted separately.
The first line contains an integer n (1≤n≤300000), the number of nodes in the tree.
The second line contains a string of n characters, each of which is "(" or ")". The j-th character is the character written in node j.
Each of the next n−1 lines contains two different integers x and y (1≤x,y≤n), the labels of two nodes joined directly by an edge.
Print the number of such ordered pairs.