RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
문제 COCI00448

Zagrade

설명

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 \(a\) is an expression, then “(\(a\))” is also an expression.
• If \(a\) and \(b\) are expressions, then “\(ab\)” is also an expression.
A tree is a structure consisting of \(n\) nodes denoted with numbers from 1 to \(n\) and \(n - 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 \(a\) and \(b\), wa,b is a
string obtained by traversing the unique path from \(a\) to \(b\) and, one by one, adding the character written
in the node we’re passing through. The string \(w_{a,b}\) also contains the character written in the node \(a\) (at
the first position) and the character written in the node \(b\) (at the last position).
Find the total number of pairs of different nodes \(a\) and \(b\) such that \(w_{a,b}\) is a correct expression.

제약
입력 형식

The first line of contains the an integer \(n\) — the number of nodes in the tree. The following line contains
an n-character string where each character is either “)” or “(”, the \(j^{th}\) character in the string is the
character written in the node \(j\). Each of the following \(n - 1\) lines contains two different positive integers \(x\)
and \(y\) (\(1 \le x\), \(y \le n\)) — the labels of nodes directly connected with an edge.

출력 형식

Output the required number of pairs.

서브태스크
서브태스크점수설명

1

10점

\(n \le 1\,000\)

2

30점

\(n \le 300\,000\), the tree is a chain — each node \(x = 1\), . . . , \(n - 1\) is connected to node \(x + 1\).

3

60점

\(n \le 300\,000\)

예제 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
문제 정보

생성자가 기록되지 않았습니다.

출처 COI 2017

평가 및 의견

Zagrade

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Zagrade

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8