#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
int n, sz[N], big[N], c[N], del[N];
vector <int> g[N];
void dfs(int u, int p) {
sz[u] = 1, big[u] = 0;
for (auto v: g[u]) {
if (v == p || del[v]) continue;
dfs(v, u);
sz[u] += sz[v];
if (sz[v] >= sz[big[u]]) big[u] = v;
}
}
unordered_map <int, int> cnt;
int centroid(int u) {
dfs(u, 0);
int num = sz[u] / 2;
while (sz[big[u]] > num) u = big[u];
return u;
}
void add(int u, int p, int s, int d) {
if (d >= 0) ++cnt[s];
for (auto v: g[u]) {
if (v == p || del[v]) continue;
add(v, u, s + c[v], min(d + c[v], c[v]));
}
}
long long ans;
void calc(int u, int p, int s, int d) {
if (d >= s) ans += cnt[- s];
for (auto v: g[u]) {
if (v == p || del[v]) continue;
calc(v, u, s + c[v], min(d, s + c[v]));
}
}
void build(int u) {
u = centroid(u);
cnt.clear();
if (c[u] >= 0) ++cnt[c[u]];
for (auto v: g[u]) {
if (del[v]) continue;
calc(v, u, c[v], min(0, c[v]));
add(v, u, c[u] + c[v], min({0, c[u] + c[v], c[v]}));
}
cnt.clear();
reverse(g[u].begin(), g[u].end());
for (auto v: g[u]) {
if (del[v]) continue;
calc(v, u, c[v], min(0, c[v]));
add(v, u, c[u] + c[v], min({0, c[u] + c[v], c[v]}));
}
ans += cnt[0];
del[u] = 1;
for (auto v: g[u])
if (!del[v]) build(v);
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int u = 1; u <= n; ++u) {
char ch;
cin >> ch;
c[u] = (ch == '(' ? 1 : -1);
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
build(1);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7384 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7456 KB |
Output is correct |
6 |
Correct |
5 ms |
7388 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7388 KB |
Output is correct |
9 |
Correct |
4 ms |
7424 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
563 ms |
53784 KB |
Output is correct |
2 |
Execution timed out |
3090 ms |
55560 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7384 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7456 KB |
Output is correct |
6 |
Correct |
5 ms |
7388 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7388 KB |
Output is correct |
9 |
Correct |
4 ms |
7424 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
563 ms |
53784 KB |
Output is correct |
12 |
Execution timed out |
3090 ms |
55560 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |