#include<bits/stdc++.h>
using namespace std;
#define int long long
#define DEBUG false
vector<vector<int> > g;
string s;
int n;
vector<int> alive;
int ans = 0;
vector<int> sz;
void dfs1(int cur, int par){
if(DEBUG) cout<<"INSIDE "<<cur<<endl;
sz[cur] = 1;
for(int u: g[cur]){
if(u == par || alive[u] == false) continue;
dfs1(u, cur);
sz[cur] += sz[u];
}
}
int find(int cur, int par, int size){
int big = -1;
for(int u: g[cur]){
if(u == par || alive[u] == false) continue;
if(big == -1 || sz[u] > sz[big]){
big = u;
}
}
if(big == -1 || sz[big]*2 <= size) return cur;
else return find(big, cur, size);
}
vector<int> f;
vector<vector<int> > subf;
int off = 0;
void dfs2(int cur, int par, int val, int low, int part){
val += s[cur] == '('?1:-1;
low = min(low, val);
if(low == val){
f[val + off]++;
subf[part][val + subf[part].size()/2]++;
}
for(int u: g[cur]){
if(u == par || alive[u] == false) continue;
dfs2(u, cur, val, low, part);
}
}
void dfs3(int cur, int par, int val, int low, int part){
int cval = s[cur] == '('?1:-1;
val += cval;
low = min(low+cval, cval);
if(low >= 0){
ans += f[-val + off] - subf[part][-val + subf[part].size()/2];
if(DEBUG)cout<<"INC ANS "<<cur<<" "<<f[-val + off] - subf[part][-val + subf[part].size()/2]<<endl;
}
for(int u: g[cur]){
if(u == par || alive[u] == false) continue;
dfs3(u, cur, val, low, part);
}
}
void decomp(int cur){
if(DEBUG)cout<<"CUR : "<<cur<<endl;
dfs1(cur, -1);
int cent = find(cur, -1, sz[cur]);
if(DEBUG)cout<<"CENT "<<cent<<endl;
f = vector<int>(sz[cent]*2+1, 0);
off = sz[cent];
// cout<<"SZ: "<<g[cent].size()<<" "<<subf.size()<<endl;
int val = s[cent] == '('?1:-1;
f[val + off]++;
for(int i = 0; i < g[cent].size(); i++){
if(!alive[g[cent][i]]) continue;
subf[i] = vector<int>(sz[g[cent][i]]*2+10, 0);
dfs2(g[cent][i], cent, val, min(val, 0LL), i);
}
for(int i = 0; i < g[cent].size(); i++){
if(!alive[g[cent][i]]) continue;
dfs3(g[cent][i], cent, 0, 0, i);
}
ans += f[0 + off];
alive[cent] = false;
for(int u: g[cent]){
if(alive[u]) decomp(u);
}
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
cin>>s;
g.resize(n);
for(int i = 0; i < n-1; i++){
int u, v; cin>>u>>v; u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
alive = vector<int>(n, true);
ans = 0;
sz = vector<int>(n, 0);
subf = vector<vector<int> > (2*n);
decomp(0);
cout<<ans<<endl;
}
Compilation message
zagrade.cpp: In function 'void decomp(long long int)':
zagrade.cpp:79:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[cent].size(); i++){
~~^~~~~~~~~~~~~~~~
zagrade.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[cent].size(); i++){
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
493 ms |
62996 KB |
Output is correct |
2 |
Correct |
524 ms |
67084 KB |
Output is correct |
3 |
Correct |
493 ms |
67084 KB |
Output is correct |
4 |
Correct |
528 ms |
67212 KB |
Output is correct |
5 |
Correct |
493 ms |
67268 KB |
Output is correct |
6 |
Correct |
492 ms |
67084 KB |
Output is correct |
7 |
Correct |
489 ms |
67148 KB |
Output is correct |
8 |
Correct |
492 ms |
67208 KB |
Output is correct |
9 |
Correct |
488 ms |
67084 KB |
Output is correct |
10 |
Correct |
490 ms |
67112 KB |
Output is correct |
11 |
Correct |
523 ms |
67084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
493 ms |
62996 KB |
Output is correct |
12 |
Correct |
524 ms |
67084 KB |
Output is correct |
13 |
Correct |
493 ms |
67084 KB |
Output is correct |
14 |
Correct |
528 ms |
67212 KB |
Output is correct |
15 |
Correct |
493 ms |
67268 KB |
Output is correct |
16 |
Correct |
492 ms |
67084 KB |
Output is correct |
17 |
Correct |
489 ms |
67148 KB |
Output is correct |
18 |
Correct |
492 ms |
67208 KB |
Output is correct |
19 |
Correct |
488 ms |
67084 KB |
Output is correct |
20 |
Correct |
490 ms |
67112 KB |
Output is correct |
21 |
Correct |
523 ms |
67084 KB |
Output is correct |
22 |
Correct |
873 ms |
52912 KB |
Output is correct |
23 |
Correct |
877 ms |
52620 KB |
Output is correct |
24 |
Correct |
862 ms |
53488 KB |
Output is correct |
25 |
Correct |
1070 ms |
51380 KB |
Output is correct |
26 |
Correct |
569 ms |
55944 KB |
Output is correct |
27 |
Correct |
571 ms |
53260 KB |
Output is correct |
28 |
Correct |
581 ms |
51980 KB |
Output is correct |
29 |
Correct |
500 ms |
67128 KB |
Output is correct |
30 |
Correct |
493 ms |
67084 KB |
Output is correct |
31 |
Correct |
174 ms |
66856 KB |
Output is correct |
32 |
Correct |
485 ms |
67080 KB |
Output is correct |