#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 3e5 + 5;
int a[N];
int sz[N];
int cnt[N];
bool blocked[N];
vector<int> g[N];
void find_sizes(int v, int p) {
sz[v] = 1;
for (int u : g[v]) {
if (!blocked[u] && u != p) {
find_sizes(u, v);
sz[v] += sz[u];
}
}
}
int find_centroid(int v, int p, int x) {
for (int u : g[v]) {
if (!blocked[u] && u != p && sz[u] > x / 2) {
return find_centroid(u, v, x);
}
}
return v;
}
void dfs(long long& ret, int v, int p, int sum, int mx, int upd, int rev) {
sum += a[v] * rev;
mx = max(mx, sum);
if (sum == mx) {
if (!upd) {
ret += cnt[sum];
} else {
cnt[sum] += upd;
}
}
for (int u : g[v]) {
if (!blocked[u] && u != p) {
dfs(ret, u, v, sum, mx, upd, rev);
}
}
}
long long solve(int root) {
find_sizes(root, root);
int centroid = find_centroid(root, root, sz[root]);
blocked[centroid] = true;
long long ret = 0;
if (a[centroid] == 1) cnt[1] = 1;
for (int u : g[centroid]) {
if (!blocked[u]) {
dfs(ret, u, centroid, 0, 0, 0, -1);
dfs(ret, u, centroid, a[centroid], max(0, a[centroid]), 1, 1);
}
}
fill(cnt, cnt + sz[root] + 1, 0);
if (a[centroid] == -1) cnt[1] = 1;
for (int u : g[centroid]) {
if (!blocked[u]) {
dfs(ret, u, centroid, 0, 0, 0, 1);
dfs(ret, u, centroid, -a[centroid], max(0, -a[centroid]), 1, -1);
}
}
fill(cnt, cnt + sz[root] + 1, 0);
for (int u : g[centroid]) {
if (!blocked[u]) {
ret += solve(u);
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
char c;
cin >> c;
a[i] = (c == '(' ? 1 : -1);
}
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cout << solve(1) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10844 KB |
Output is correct |
3 |
Correct |
4 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
3 ms |
10940 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
3 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
334 ms |
48352 KB |
Output is correct |
2 |
Correct |
345 ms |
48212 KB |
Output is correct |
3 |
Correct |
332 ms |
48212 KB |
Output is correct |
4 |
Correct |
355 ms |
48212 KB |
Output is correct |
5 |
Correct |
333 ms |
48364 KB |
Output is correct |
6 |
Correct |
366 ms |
48212 KB |
Output is correct |
7 |
Correct |
333 ms |
48580 KB |
Output is correct |
8 |
Correct |
343 ms |
48192 KB |
Output is correct |
9 |
Correct |
338 ms |
48364 KB |
Output is correct |
10 |
Correct |
305 ms |
48544 KB |
Output is correct |
11 |
Correct |
323 ms |
48540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10844 KB |
Output is correct |
3 |
Correct |
4 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
3 ms |
10940 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
3 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
334 ms |
48352 KB |
Output is correct |
12 |
Correct |
345 ms |
48212 KB |
Output is correct |
13 |
Correct |
332 ms |
48212 KB |
Output is correct |
14 |
Correct |
355 ms |
48212 KB |
Output is correct |
15 |
Correct |
333 ms |
48364 KB |
Output is correct |
16 |
Correct |
366 ms |
48212 KB |
Output is correct |
17 |
Correct |
333 ms |
48580 KB |
Output is correct |
18 |
Correct |
343 ms |
48192 KB |
Output is correct |
19 |
Correct |
338 ms |
48364 KB |
Output is correct |
20 |
Correct |
305 ms |
48544 KB |
Output is correct |
21 |
Correct |
323 ms |
48540 KB |
Output is correct |
22 |
Correct |
539 ms |
24832 KB |
Output is correct |
23 |
Correct |
474 ms |
25064 KB |
Output is correct |
24 |
Correct |
491 ms |
25064 KB |
Output is correct |
25 |
Correct |
495 ms |
24912 KB |
Output is correct |
26 |
Correct |
387 ms |
32896 KB |
Output is correct |
27 |
Correct |
380 ms |
29200 KB |
Output is correct |
28 |
Correct |
389 ms |
28312 KB |
Output is correct |
29 |
Correct |
307 ms |
48212 KB |
Output is correct |
30 |
Correct |
362 ms |
48552 KB |
Output is correct |
31 |
Correct |
67 ms |
24464 KB |
Output is correct |
32 |
Correct |
306 ms |
48212 KB |
Output is correct |