#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int fwd[300001], rev[300001], subtree[300001];
vector<int> graph[300001];
ll ans = 0;
bool processed[300001];
int get_subtree_sizes(int node, int parent = 0) {
for (int i : graph[node])
if (i != parent && !processed[i])
subtree[node] += get_subtree_sizes(i, node);
return ++subtree[node];
}
int get_centroid(int node, int target, int parent = 0) {
for (int i : graph[node])
if (i != parent && !processed[i] && subtree[i] > target)
return get_centroid(i, target, node);
return node;
}
void calc(int node, int parent, int *v, vector<int> &cnt, int curr = 0,
int mn = 0) {
curr += v[node];
if (v[node] == 1)
mn = min(0, mn + 1);
else
mn--;
if (!mn) ans += cnt[curr];
for (int i : graph[node])
if (i != parent && !processed[i]) calc(i, node, v, cnt, curr, mn);
}
void upd(int node, int parent, int *v, vector<int> &cnt, int curr, int mn) {
curr += v[node];
if (v[node] == 1)
mn = min(0, mn + 1);
else
mn--;
if (!mn) cnt[curr]++;
for (int i : graph[node])
if (i != parent && !processed[i]) upd(i, node, v, cnt, curr, mn);
}
void decompose(int node = 1) {
int sz = get_subtree_sizes(node);
int centroid = get_centroid(node, sz / 2);
vector<int> cnt_fwd(sz + 1), cnt_rev(sz + 1);
if (fwd[centroid] == 1)
cnt_fwd[1]++;
else
cnt_rev[1]++;
for (int i : graph[centroid])
if (!processed[i]) {
calc(i, centroid, fwd, cnt_rev);
calc(i, centroid, rev, cnt_fwd);
upd(i, centroid, fwd, cnt_fwd, fwd[centroid],
min(0, fwd[centroid]));
upd(i, centroid, rev, cnt_rev, rev[centroid],
min(0, rev[centroid]));
}
processed[centroid] = true;
for (int i : graph[centroid])
if (!processed[i]) decompose(i);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
if (c == '(')
fwd[i] = 1, rev[i] = -1;
else
fwd[i] = -1, rev[i] = 1;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
decompose();
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7740 KB |
Output is correct |
2 |
Correct |
6 ms |
7756 KB |
Output is correct |
3 |
Correct |
6 ms |
7628 KB |
Output is correct |
4 |
Correct |
6 ms |
7756 KB |
Output is correct |
5 |
Correct |
6 ms |
7628 KB |
Output is correct |
6 |
Correct |
6 ms |
7756 KB |
Output is correct |
7 |
Correct |
6 ms |
7852 KB |
Output is correct |
8 |
Correct |
6 ms |
7756 KB |
Output is correct |
9 |
Correct |
6 ms |
7628 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
194 ms |
83908 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7740 KB |
Output is correct |
2 |
Correct |
6 ms |
7756 KB |
Output is correct |
3 |
Correct |
6 ms |
7628 KB |
Output is correct |
4 |
Correct |
6 ms |
7756 KB |
Output is correct |
5 |
Correct |
6 ms |
7628 KB |
Output is correct |
6 |
Correct |
6 ms |
7756 KB |
Output is correct |
7 |
Correct |
6 ms |
7852 KB |
Output is correct |
8 |
Correct |
6 ms |
7756 KB |
Output is correct |
9 |
Correct |
6 ms |
7628 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Runtime error |
194 ms |
83908 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |