| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 (추가 시간 없음) | 2048 MB | 80 | 41 | 35 | 47.297% |
You are given a tree where each node is annotated with a character from ()[]{}. A path is a sequence of one or more nodes where no node is repeated and every pair of adjacent nodes is connected with an edge. A path is balanced if the characters at each node, when concatenated, form a balanced string. A string is balanced if it satisfies the following definition:
($s$), [$s$], and {$s$} are balanced strings.Compute the number of balanced paths over the entire tree.
The first line of input contains a single integer $n$ ($2 \le n \le 5 \cdot 10^3$).
The next line contains a string of $n$ characters, where each character is one of ()[]\{\}.
Each of the next $n-1$ lines contains two integers, $u$ and $v$ ($1 \le u < v \le n$), indicating that nodes $u$ and $v$ are connected with an edge. It is guaranteed the graph is a tree.
Output a single integer, which is the number of balanced paths over the entire tree.
4 ()() 1 2 2 3 3 4
4
4 [[]] 1 2 2 3 3 4
2
6
([]{})
1 2
2 3
3 4
4 5
5 6
4
ICPC > Regionals > North America > Pacific Northwest Regional > 2023 ICPC Pacific Northwest Region > Division 2 I번
ICPC > Regionals > North America > Mid-Central Regional > 2023 Mid-Central USA Programming Contest J번
ICPC > Regionals > North America > South Central USA Regional > 2023 South Central USA Regional Contest > Division 1 B번
ICPC > Regionals > North America > South Central USA Regional > 2023 South Central USA Regional Contest > Division 2 B번
ICPC > Regionals > North America > Mid-Atlantic Regional > 2023 Mid-Atlantic USA Regional Contest > Division 1 B번
ICPC > Regionals > North America > Mid-Atlantic Regional > 2023 Mid-Atlantic USA Regional Contest > Division 2 B번
ICPC > Regionals > North America > Southeast USA Regional > 2023 Southeast USA Regional Programming Contest > Division 1 B번
ICPC > Regionals > North America > Southeast USA Regional > 2023 Southeast USA Regional Programming Contest > Division 2 B번