#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 3e5 + 5, inf = 1e9;
int N;
string s;
vector<int> g[MX];
int par[MX], bal[MX];
set<pair<int, int>> path[MX][2]; // open, close
// (balance, count)
int find(int v) {
return par[v] == v ? v : par[v] = find(par[v]);
}
ll ans = 0;
void dfs(int v, int p) {
for(auto u : g[v]) {
if(u == p) continue;
// build
bal[u] = bal[v] + (s[u] == '(' ? 1 : -1);
dfs(u, v);
}
int c = s[v] == '(';
for(auto u : g[v]) {
if(u == p) continue;
// erase invalid
int pu = find(u);
auto it = path[pu][c].lower_bound({bal[v], -inf});
if(it != path[pu][c].end() && it->first == bal[v]) {
path[pu][c].erase(it);
}
}
// straight path
for(auto u : g[v]) {
if(u == p) continue;
int pu = find(u);
auto it = path[pu][c].lower_bound({bal[p], -inf});
if(it != path[pu][c].end() && it->first == bal[p]) {
ans += it->second;
}
}
// calc answer, merging
for(auto u : g[v]) {
if(u == p) continue;
int pu = find(u), pv = find(v);
if((path[pu][0].size() + path[pu][1].size()) > (path[pv][0].size() + path[pv][1].size())) {
swap(pu, pv);
}
for(int t = 0; t < 2; t++) {
// calc answer
for(auto [b, c] : path[pu][t]) {
int k = b - bal[p];
k = -k + bal[v];
auto it = path[pv][t ^ 1].lower_bound({k, -inf});
if(it != path[pv][t ^ 1].end() && it->first == k) {
ans += 1LL * c * it->second;
}
}
}
par[pu] = pv;
for(int t = 0; t < 2; t++) {
// merging
for(auto x : path[pu][t]) {
auto it = path[pv][t].lower_bound({x.first, -inf});
if(it != path[pv][t].end() && it->first == x.first) {
path[pv][t].insert({x.first, x.second + it->second});
path[pv][t].erase(it);
} else {
path[pv][t].insert(x);
}
}
}
path[pu][0].clear();
path[pu][1].clear();
}
// add new path
int pv = find(v);
auto it = path[pv][c ^ 1].lower_bound({bal[v], -inf});
if(it != path[pv][c ^ 1].end() && it->first == bal[v]) {
path[pv][c ^ 1].insert({it->first, it->second + 1});
path[pv][c ^ 1].erase(*it);
} else {
path[pv][c ^ 1].insert({bal[v], 1});
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> s;
s = '#' + s;
for(int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1; i <= N; i++) par[i] = i;
bal[1] = (s[1] == '(' ? 1 : -1);
dfs(1, 0);
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
16 ms |
35512 KB |
Output is correct |
3 |
Correct |
16 ms |
35540 KB |
Output is correct |
4 |
Correct |
16 ms |
35492 KB |
Output is correct |
5 |
Correct |
16 ms |
35500 KB |
Output is correct |
6 |
Correct |
16 ms |
35600 KB |
Output is correct |
7 |
Correct |
16 ms |
35520 KB |
Output is correct |
8 |
Correct |
16 ms |
35596 KB |
Output is correct |
9 |
Correct |
17 ms |
35572 KB |
Output is correct |
10 |
Correct |
16 ms |
35540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
118020 KB |
Output is correct |
2 |
Correct |
160 ms |
123624 KB |
Output is correct |
3 |
Correct |
146 ms |
118040 KB |
Output is correct |
4 |
Correct |
154 ms |
122188 KB |
Output is correct |
5 |
Correct |
147 ms |
118092 KB |
Output is correct |
6 |
Correct |
160 ms |
120120 KB |
Output is correct |
7 |
Correct |
152 ms |
118016 KB |
Output is correct |
8 |
Correct |
153 ms |
120120 KB |
Output is correct |
9 |
Correct |
148 ms |
118036 KB |
Output is correct |
10 |
Correct |
213 ms |
125176 KB |
Output is correct |
11 |
Correct |
121 ms |
117944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
16 ms |
35512 KB |
Output is correct |
3 |
Correct |
16 ms |
35540 KB |
Output is correct |
4 |
Correct |
16 ms |
35492 KB |
Output is correct |
5 |
Correct |
16 ms |
35500 KB |
Output is correct |
6 |
Correct |
16 ms |
35600 KB |
Output is correct |
7 |
Correct |
16 ms |
35520 KB |
Output is correct |
8 |
Correct |
16 ms |
35596 KB |
Output is correct |
9 |
Correct |
17 ms |
35572 KB |
Output is correct |
10 |
Correct |
16 ms |
35540 KB |
Output is correct |
11 |
Correct |
149 ms |
118020 KB |
Output is correct |
12 |
Correct |
160 ms |
123624 KB |
Output is correct |
13 |
Correct |
146 ms |
118040 KB |
Output is correct |
14 |
Correct |
154 ms |
122188 KB |
Output is correct |
15 |
Correct |
147 ms |
118092 KB |
Output is correct |
16 |
Correct |
160 ms |
120120 KB |
Output is correct |
17 |
Correct |
152 ms |
118016 KB |
Output is correct |
18 |
Correct |
153 ms |
120120 KB |
Output is correct |
19 |
Correct |
148 ms |
118036 KB |
Output is correct |
20 |
Correct |
213 ms |
125176 KB |
Output is correct |
21 |
Correct |
121 ms |
117944 KB |
Output is correct |
22 |
Correct |
223 ms |
47860 KB |
Output is correct |
23 |
Correct |
236 ms |
48012 KB |
Output is correct |
24 |
Correct |
223 ms |
47892 KB |
Output is correct |
25 |
Correct |
227 ms |
47900 KB |
Output is correct |
26 |
Correct |
158 ms |
74764 KB |
Output is correct |
27 |
Correct |
158 ms |
63324 KB |
Output is correct |
28 |
Correct |
162 ms |
58668 KB |
Output is correct |
29 |
Correct |
188 ms |
124948 KB |
Output is correct |
30 |
Correct |
193 ms |
125036 KB |
Output is correct |
31 |
Correct |
92 ms |
55732 KB |
Output is correct |
32 |
Correct |
119 ms |
117972 KB |
Output is correct |