#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N = 3e5 + 5;
vector < int > V[N];
int sz[N],U[N],T[N+N],A[N],C,n,i,x,y;
char s[N];
ll ans;
void f(int x, int p, int n){
sz[x] = 1; int h=1;
for(auto y : V[x]){
if(U[y] || y == p) continue;
f(y,x,n);
sz[x] += sz[y];
if(sz[y] > n/2) h=0;
}
if(h && n-sz[x] <= n/2) C=x;
}
vector < int > v[N];
int node;
void g(int x, int p, int t, int mn, int mx){
sz[x] = 1;
t += A[x];
mn = min(mn , t);
mx = max(mx , t);
if(!node && t == mx && A[x] == 1) ans += T[t];
if(node && t == mn && t <= 0) { v[node].pb(-t); T[-t]++; }
for(auto y : V[x]){
if(U[y] || y == p) continue;
g(y,x,t,mn,mx);
sz[x] += sz[y];
}
}
void slv(int x, int n){
f(x,0,n);
x = C;
U[x] = 1;
if(A[x] == -1) T[1]++;
for(auto y : V[x]){
if(U[y]) continue;
node = y;
g(y,x,A[x],A[x],A[x]);
}
node = 0;
if(A[x] == 1) ans += T[0];
for(auto y : V[x]){
if(U[y]) continue;
for(auto t : v[y]) T[t]--;
g(y,x,0,0,0);
for(auto t : v[y]) T[t]++;
}
if(A[x] == -1) T[1]--;
for(auto y : V[x]){
if(U[y]) continue;
for(auto t : v[y]) T[t]--;
v[y].clear();
}
//cout << x << " " << ans << " ss\n";
//exit(0);
for(auto y : V[x]) if(!U[y]) slv(y,sz[y]);
}
int main(){
scanf("%d %s",&n,s+1);
for(i=1;i<=n;i++) A[i] = s[i] == '(' ? 1 : -1;
for(i=2;i<=n;i++){
scanf("%d%d",&x,&y);
V[x].pb(y); V[y].pb(x);
}
slv(1,n);
printf("%lld",ans);
return 0;
}
Compilation message
zagrade.cpp: In function 'int main()':
zagrade.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %s",&n,s+1);
~~~~~^~~~~~~~~~~~~~~~
zagrade.cpp:82:14: 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 |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14592 KB |
Output is correct |
3 |
Correct |
14 ms |
14664 KB |
Output is correct |
4 |
Correct |
15 ms |
14700 KB |
Output is correct |
5 |
Correct |
16 ms |
14700 KB |
Output is correct |
6 |
Correct |
14 ms |
14872 KB |
Output is correct |
7 |
Correct |
15 ms |
14872 KB |
Output is correct |
8 |
Correct |
15 ms |
14872 KB |
Output is correct |
9 |
Correct |
14 ms |
14872 KB |
Output is correct |
10 |
Correct |
14 ms |
14872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
495 ms |
56752 KB |
Output is correct |
2 |
Correct |
550 ms |
69100 KB |
Output is correct |
3 |
Correct |
504 ms |
69100 KB |
Output is correct |
4 |
Correct |
553 ms |
69100 KB |
Output is correct |
5 |
Correct |
499 ms |
69100 KB |
Output is correct |
6 |
Correct |
519 ms |
69100 KB |
Output is correct |
7 |
Correct |
482 ms |
69100 KB |
Output is correct |
8 |
Correct |
481 ms |
69100 KB |
Output is correct |
9 |
Correct |
487 ms |
69100 KB |
Output is correct |
10 |
Correct |
465 ms |
69100 KB |
Output is correct |
11 |
Correct |
562 ms |
69100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14592 KB |
Output is correct |
3 |
Correct |
14 ms |
14664 KB |
Output is correct |
4 |
Correct |
15 ms |
14700 KB |
Output is correct |
5 |
Correct |
16 ms |
14700 KB |
Output is correct |
6 |
Correct |
14 ms |
14872 KB |
Output is correct |
7 |
Correct |
15 ms |
14872 KB |
Output is correct |
8 |
Correct |
15 ms |
14872 KB |
Output is correct |
9 |
Correct |
14 ms |
14872 KB |
Output is correct |
10 |
Correct |
14 ms |
14872 KB |
Output is correct |
11 |
Correct |
495 ms |
56752 KB |
Output is correct |
12 |
Correct |
550 ms |
69100 KB |
Output is correct |
13 |
Correct |
504 ms |
69100 KB |
Output is correct |
14 |
Correct |
553 ms |
69100 KB |
Output is correct |
15 |
Correct |
499 ms |
69100 KB |
Output is correct |
16 |
Correct |
519 ms |
69100 KB |
Output is correct |
17 |
Correct |
482 ms |
69100 KB |
Output is correct |
18 |
Correct |
481 ms |
69100 KB |
Output is correct |
19 |
Correct |
487 ms |
69100 KB |
Output is correct |
20 |
Correct |
465 ms |
69100 KB |
Output is correct |
21 |
Correct |
562 ms |
69100 KB |
Output is correct |
22 |
Correct |
1341 ms |
69100 KB |
Output is correct |
23 |
Correct |
1248 ms |
69100 KB |
Output is correct |
24 |
Correct |
1334 ms |
69100 KB |
Output is correct |
25 |
Correct |
1343 ms |
69100 KB |
Output is correct |
26 |
Correct |
537 ms |
69100 KB |
Output is correct |
27 |
Correct |
645 ms |
69100 KB |
Output is correct |
28 |
Correct |
573 ms |
69100 KB |
Output is correct |
29 |
Correct |
559 ms |
69100 KB |
Output is correct |
30 |
Correct |
502 ms |
69100 KB |
Output is correct |
31 |
Correct |
160 ms |
69100 KB |
Output is correct |
32 |
Correct |
623 ms |
69100 KB |
Output is correct |