이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
string s;
list<int> adj[N];
int n;
long long ans = 0;
map<int, int> m1, m2;
void dfs(int v, int p = -1, int d = 0) {
bool check = true;
for (int i : adj[v]) {
if (i != p) {
dfs(i, v, s[v] == ')' ? d + 1 : d - 1);
if (check) {
check = false;
if (s[v] == ')')
ans += m1[d], m2[d + 1]++;
else
ans += m2[d], m1[d - 1]++;
}
}
}
if (check) {
if (s[v] == ')')
ans += m1[d], m2[d + 1]++;
else
ans += m2[d], m1[d - 1]++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> s;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
int r = -1;
for (int i = 0; i < n; i++) {
if (adj[i].size() == 1) {
r = i;
break;
}
}
dfs(r);
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |