#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 3e5+5;
vector<pii> up[maxn];
vector<pii> down[maxn];
vector<int> tree[maxn];
int A[maxn];
int sum[maxn], subsz[maxn], col[maxn], vis[maxn], mx[maxn];
vector<int> csum[maxn];
int n;
void predfs(int x, int p){
subsz[x]=1;
for(auto v: tree[x]){
if(v == p || vis[v])continue;
predfs(v, x);
subsz[x]+=subsz[v];
}
}
int gtcen(int x, int p, int tot){
int re = 0;
mx[x]=0;
for(auto v: tree[x]){
if(v == p || vis[v])continue;
int tmp = gtcen(v, x, tot);
if(mx[tmp] < mx[re])re = tmp;
mx[x] = max(mx[x], subsz[v]);
}
mx[x]=max(mx[x], tot-subsz[x]);
if(mx[x] < mx[re])re=x;
return re;
}
void dfs(int x, int p, int ss, int mn, int mn2, int color, int rr){
//cout << x << " " << ss << endl;
if(mn2 >= 0){
up[ss+A[rr]].push_back({color, rr});
//cout << "up " << ss+A[rr] << " " << x << " " << rr << endl;
}
if(ss == mn && ss <= 0){
down[-ss].push_back({color, rr});
//cout << "down " << -ss << " " << x << " " << rr << endl;
}
for(auto v: tree[x]){
if(v == p || vis[v])continue;
dfs(v, x, ss+A[v], min(ss+A[v], mn), min(0, mn2 + A[v]), color, rr);
}
}
int build(int x){
predfs(x, 0);
x = gtcen(x, 0, subsz[x]);
vis[x]=1;
int cc = 0;
if(A[x] > 0)up[1].push_back({-1, x});
down[0].push_back({-1, x});
for(auto v: tree[x]){
if(vis[v])continue;
dfs(v, x, A[v], min(0, A[v]), min({0, A[v], A[v]+A[x]}), cc++, x);
}
csum[x].assign(cc, 0);
for(auto v: tree[x]){
if(!vis[v]){
int tmp = build(v);
//cout << x << " " << tmp << endl;
}
}
return x;
}
int main(){
ll ans = 0;
cin >> n;
mx[0]=n;
for(int i = 1; i <= n; i++){
char c;
cin >> c;
if(c == '(')A[i]=1;
else A[i]=-1;
}
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
build(1);
for(int i = 0; i <= n; i++){
for(auto v: down[i]){
sum[v.s]++;
if(v.f >= 0)csum[v.s][v.f]++;
}
for(auto v: up[i]){
ans += sum[v.s];
if(v.f >= 0)ans-=csum[v.s][v.f];
}
for(auto v: down[i]){
sum[v.s] = 0;
if(v.f >= 0)csum[v.s][v.f]=0;
}
}
cout << ans;
}
Compilation message
zagrade.cpp: In function 'int build(int)':
zagrade.cpp:74:17: warning: unused variable 'tmp' [-Wunused-variable]
74 | int tmp = build(v);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28628 KB |
Output is correct |
2 |
Correct |
14 ms |
28628 KB |
Output is correct |
3 |
Correct |
15 ms |
28656 KB |
Output is correct |
4 |
Correct |
18 ms |
28628 KB |
Output is correct |
5 |
Correct |
19 ms |
28600 KB |
Output is correct |
6 |
Correct |
15 ms |
28652 KB |
Output is correct |
7 |
Correct |
16 ms |
28616 KB |
Output is correct |
8 |
Correct |
15 ms |
28520 KB |
Output is correct |
9 |
Correct |
19 ms |
28552 KB |
Output is correct |
10 |
Correct |
16 ms |
28564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
497 ms |
91888 KB |
Output is correct |
2 |
Correct |
613 ms |
112288 KB |
Output is correct |
3 |
Correct |
562 ms |
92308 KB |
Output is correct |
4 |
Correct |
567 ms |
107084 KB |
Output is correct |
5 |
Correct |
535 ms |
92484 KB |
Output is correct |
6 |
Correct |
554 ms |
98716 KB |
Output is correct |
7 |
Correct |
475 ms |
92064 KB |
Output is correct |
8 |
Correct |
545 ms |
98728 KB |
Output is correct |
9 |
Correct |
483 ms |
91932 KB |
Output is correct |
10 |
Correct |
583 ms |
127288 KB |
Output is correct |
11 |
Correct |
596 ms |
119032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28628 KB |
Output is correct |
2 |
Correct |
14 ms |
28628 KB |
Output is correct |
3 |
Correct |
15 ms |
28656 KB |
Output is correct |
4 |
Correct |
18 ms |
28628 KB |
Output is correct |
5 |
Correct |
19 ms |
28600 KB |
Output is correct |
6 |
Correct |
15 ms |
28652 KB |
Output is correct |
7 |
Correct |
16 ms |
28616 KB |
Output is correct |
8 |
Correct |
15 ms |
28520 KB |
Output is correct |
9 |
Correct |
19 ms |
28552 KB |
Output is correct |
10 |
Correct |
16 ms |
28564 KB |
Output is correct |
11 |
Correct |
497 ms |
91888 KB |
Output is correct |
12 |
Correct |
613 ms |
112288 KB |
Output is correct |
13 |
Correct |
562 ms |
92308 KB |
Output is correct |
14 |
Correct |
567 ms |
107084 KB |
Output is correct |
15 |
Correct |
535 ms |
92484 KB |
Output is correct |
16 |
Correct |
554 ms |
98716 KB |
Output is correct |
17 |
Correct |
475 ms |
92064 KB |
Output is correct |
18 |
Correct |
545 ms |
98728 KB |
Output is correct |
19 |
Correct |
483 ms |
91932 KB |
Output is correct |
20 |
Correct |
583 ms |
127288 KB |
Output is correct |
21 |
Correct |
596 ms |
119032 KB |
Output is correct |
22 |
Correct |
982 ms |
66472 KB |
Output is correct |
23 |
Correct |
913 ms |
67404 KB |
Output is correct |
24 |
Correct |
915 ms |
67468 KB |
Output is correct |
25 |
Correct |
966 ms |
68472 KB |
Output is correct |
26 |
Correct |
542 ms |
72960 KB |
Output is correct |
27 |
Correct |
560 ms |
69488 KB |
Output is correct |
28 |
Correct |
554 ms |
67572 KB |
Output is correct |
29 |
Correct |
608 ms |
127520 KB |
Output is correct |
30 |
Correct |
583 ms |
127048 KB |
Output is correct |
31 |
Correct |
201 ms |
55544 KB |
Output is correct |
32 |
Correct |
580 ms |
118940 KB |
Output is correct |