#include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int maxn = 3e5 + 5;
int n;
char s[maxn];
int sz[maxn], cen[maxn];
vector<int> way[maxn];
long long ans;
int cnt[2][maxn];
void dfs(int t, int u, int last, int cur, int mx, vector<int> &vec) {
if(t==0) cur += s[u]=='(' ? 1 : -1;
else cur += s[u]=='(' ? -1 : 1;
mx = max(mx, cur);
if(cur==mx) vec.pb(cur);
for(auto v : way[u]) {
if(v==last || cen[v]) continue;
dfs(t,v,u,cur,mx,vec);
}
}
void solve(int u) {
int t = s[u]=='(' ? -1 : 1;
vector<int> L, R;
for(auto v : way[u]) {
if(cen[v]) continue;
vector<int> tl, tr;
dfs(0,v,u,0,0,tl);
dfs(1,v,u,t,max(t,0),tr);
for(auto x : tl) {
L.push_back(x);
ans += cnt[1][x];
if(x==t) ans++;
}
for(auto x : tr) {
R.push_back(x);
ans += cnt[0][x];
if(x==0) ans++;
}
for(auto x : tl) cnt[0][x]++;
for(auto x : tr) cnt[1][x]++;
}
for(auto x : L) cnt[0][x]--;
for(auto x : R) cnt[1][x]--;
}
void survey(int u, int last) {
// printf("survey %d %d\n",u,last);
sz[u] = 1;
for(auto v : way[u]) {
if(v==last || cen[v]) continue;
survey(v, u);
sz[u] += sz[v];
}
}
void decom(int u) {
survey(u, 0);
int x = u, last = 0;
redo:
for(auto v : way[x]) {
if(v==last || cen[v]) continue;
if(sz[v]>sz[u]/2) {
last = x; x = v;
goto redo;
}
}
solve(x);
cen[x] = 1;
for(auto v : way[x]) {
if(cen[v]) continue;
decom(v);
}
}
int main() {
int x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf(" %c",&s[i]);
for(int i=0;i<n-1;i++) {
scanf("%d%d",&x,&y);
way[x].pb(y); way[y].pb(x);
}
decom(1);
printf("%lld",ans);
}
Compilation message
zagrade.cpp: In function 'int main()':
zagrade.cpp:82:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
zagrade.cpp:83:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf(" %c",&s[i]);
^
zagrade.cpp:85:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&x,&y);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7484 KB |
Output is correct |
4 |
Correct |
8 ms |
7644 KB |
Output is correct |
5 |
Correct |
7 ms |
7644 KB |
Output is correct |
6 |
Correct |
8 ms |
7644 KB |
Output is correct |
7 |
Correct |
8 ms |
7644 KB |
Output is correct |
8 |
Correct |
8 ms |
7644 KB |
Output is correct |
9 |
Correct |
8 ms |
7644 KB |
Output is correct |
10 |
Correct |
7 ms |
7772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
483 ms |
38624 KB |
Output is correct |
2 |
Correct |
577 ms |
39936 KB |
Output is correct |
3 |
Correct |
532 ms |
39936 KB |
Output is correct |
4 |
Correct |
542 ms |
39936 KB |
Output is correct |
5 |
Correct |
479 ms |
39936 KB |
Output is correct |
6 |
Correct |
544 ms |
39936 KB |
Output is correct |
7 |
Correct |
519 ms |
39936 KB |
Output is correct |
8 |
Correct |
481 ms |
39936 KB |
Output is correct |
9 |
Correct |
470 ms |
39936 KB |
Output is correct |
10 |
Correct |
483 ms |
41228 KB |
Output is correct |
11 |
Correct |
528 ms |
41228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7484 KB |
Output is correct |
4 |
Correct |
8 ms |
7644 KB |
Output is correct |
5 |
Correct |
7 ms |
7644 KB |
Output is correct |
6 |
Correct |
8 ms |
7644 KB |
Output is correct |
7 |
Correct |
8 ms |
7644 KB |
Output is correct |
8 |
Correct |
8 ms |
7644 KB |
Output is correct |
9 |
Correct |
8 ms |
7644 KB |
Output is correct |
10 |
Correct |
7 ms |
7772 KB |
Output is correct |
11 |
Correct |
483 ms |
38624 KB |
Output is correct |
12 |
Correct |
577 ms |
39936 KB |
Output is correct |
13 |
Correct |
532 ms |
39936 KB |
Output is correct |
14 |
Correct |
542 ms |
39936 KB |
Output is correct |
15 |
Correct |
479 ms |
39936 KB |
Output is correct |
16 |
Correct |
544 ms |
39936 KB |
Output is correct |
17 |
Correct |
519 ms |
39936 KB |
Output is correct |
18 |
Correct |
481 ms |
39936 KB |
Output is correct |
19 |
Correct |
470 ms |
39936 KB |
Output is correct |
20 |
Correct |
483 ms |
41228 KB |
Output is correct |
21 |
Correct |
528 ms |
41228 KB |
Output is correct |
22 |
Correct |
893 ms |
41228 KB |
Output is correct |
23 |
Correct |
902 ms |
41228 KB |
Output is correct |
24 |
Correct |
846 ms |
41228 KB |
Output is correct |
25 |
Correct |
955 ms |
41228 KB |
Output is correct |
26 |
Correct |
580 ms |
46584 KB |
Output is correct |
27 |
Correct |
604 ms |
47920 KB |
Output is correct |
28 |
Correct |
600 ms |
51316 KB |
Output is correct |
29 |
Correct |
490 ms |
74504 KB |
Output is correct |
30 |
Correct |
480 ms |
78556 KB |
Output is correct |
31 |
Correct |
150 ms |
78556 KB |
Output is correct |
32 |
Correct |
525 ms |
84376 KB |
Output is correct |