#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define MAXN 300005
using namespace std;
typedef long long ll;
ll n;
char s[MAXN];
ll zagrada[MAXN];
vector<ll> niz[MAXN];
ll obradjen[MAXN];
ll podst[MAXN];
ll cale[MAXN];
ll neg[MAXN],poz[MAXN];
ll res=0;
vector<ll> dodaj;
ll dfsV(ll t,ll o){
podst[t]=1;
cale[t]=o;
for(auto x:niz[t])
if(x!=o && !obradjen[x])
podst[t]+=dfsV(x,t);
return podst[t];
}
void dfsR(ll t,ll o,ll mi,ll ma,ll vkorena,ll vrednost){
vrednost+=zagrada[t];
if(vrednost>0 && ma<=vrednost){
//if(neg[vrednost])printf("k %lld %lld\n",t,vrednost);
res+=neg[vrednost];
dodaj.pb(vrednost-vkorena);
}
if(vrednost<0 && mi>=vrednost){
//if(poz[-vrednost])printf("k %lld %lld\n",t,vrednost);
res+=poz[-vrednost];
dodaj.pb(vrednost-vkorena);
}
if(vrednost==0){
if(mi==0){
//printf("dodao %lld\n",t);
res+=1+(neg[0]+poz[0]);
dodaj.pb(-1);
}
if(ma==0){
//printf("dodao %lld\n",t);
res+=1+(neg[0]+poz[0]);
dodaj.pb(1);
}
}
for(auto x:niz[t]){
if(obradjen[x]|| x==o)continue;
dfsR(x,t,min(mi,vrednost),max(ma,vrednost),vkorena,vrednost);
}
}
void centroidna(ll c){
dfsV(c,0);
ll vel=podst[c];
for(ll i=0;i<=vel+1;i++){
neg[i]=0;
poz[i]=0;
}
if(vel==1){
//printf(" jedan %lld\n",c);
obradjen[c]=true;
return;
}
while(22){
bool naso=false;
for(auto x:niz[c]){
if(obradjen[x] || x==cale[c])continue;
if(podst[x]>(vel>>1)){
c=x;
naso=true;
break;
}
}
if(!naso)break;
}
/// OVDE IDE OBRADA
for(auto x:niz[c]){
if(obradjen[x])continue;
dfsR(x,c,min(0ll,zagrada[c]),max(0ll,zagrada[c]),zagrada[c],zagrada[c]);
for(auto y:dodaj){
if(y>=0)poz[y]++;
else neg[-y]++;
}
dodaj.clear();
}
///OVDE JE KRAJ OBRADE
//printf(" %lld %lld\n",c,res);
obradjen[c]=true;
for(auto x:niz[c]){
if(!obradjen[x])centroidna(x);
}
}
int main()
{
scanf("%lld", &n);
scanf("%s",s);
for(ll i=1;i<=n;i++)
zagrada[i]=(s[i-1]=='('?1:-1);
for(ll i=1;i<n;i++){
ll t1,t2;
scanf("%lld %lld", &t1, &t2);
niz[t1].pb(t2);
niz[t2].pb(t1);
}
centroidna(1);
printf("%lld",res);
return 0;
}
Compilation message
zagrade.cpp: In function 'int main()':
zagrade.cpp:111:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &n);
~~~~~^~~~~~~~~~~~
zagrade.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s);
~~~~~^~~~~~~~
zagrade.cpp:117:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &t1, &t2);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7420 KB |
Output is correct |
2 |
Correct |
11 ms |
7544 KB |
Output is correct |
3 |
Correct |
10 ms |
7544 KB |
Output is correct |
4 |
Correct |
9 ms |
7544 KB |
Output is correct |
5 |
Correct |
9 ms |
7544 KB |
Output is correct |
6 |
Correct |
10 ms |
7544 KB |
Output is correct |
7 |
Correct |
9 ms |
7544 KB |
Output is correct |
8 |
Correct |
9 ms |
7544 KB |
Output is correct |
9 |
Correct |
11 ms |
7544 KB |
Output is correct |
10 |
Correct |
9 ms |
7544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
49084 KB |
Output is correct |
2 |
Correct |
314 ms |
50160 KB |
Output is correct |
3 |
Correct |
308 ms |
49404 KB |
Output is correct |
4 |
Correct |
310 ms |
50152 KB |
Output is correct |
5 |
Correct |
305 ms |
49400 KB |
Output is correct |
6 |
Correct |
302 ms |
49904 KB |
Output is correct |
7 |
Correct |
299 ms |
49400 KB |
Output is correct |
8 |
Correct |
309 ms |
49904 KB |
Output is correct |
9 |
Correct |
291 ms |
49400 KB |
Output is correct |
10 |
Correct |
305 ms |
50852 KB |
Output is correct |
11 |
Correct |
296 ms |
50660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7420 KB |
Output is correct |
2 |
Correct |
11 ms |
7544 KB |
Output is correct |
3 |
Correct |
10 ms |
7544 KB |
Output is correct |
4 |
Correct |
9 ms |
7544 KB |
Output is correct |
5 |
Correct |
9 ms |
7544 KB |
Output is correct |
6 |
Correct |
10 ms |
7544 KB |
Output is correct |
7 |
Correct |
9 ms |
7544 KB |
Output is correct |
8 |
Correct |
9 ms |
7544 KB |
Output is correct |
9 |
Correct |
11 ms |
7544 KB |
Output is correct |
10 |
Correct |
9 ms |
7544 KB |
Output is correct |
11 |
Correct |
309 ms |
49084 KB |
Output is correct |
12 |
Correct |
314 ms |
50160 KB |
Output is correct |
13 |
Correct |
308 ms |
49404 KB |
Output is correct |
14 |
Correct |
310 ms |
50152 KB |
Output is correct |
15 |
Correct |
305 ms |
49400 KB |
Output is correct |
16 |
Correct |
302 ms |
49904 KB |
Output is correct |
17 |
Correct |
299 ms |
49400 KB |
Output is correct |
18 |
Correct |
309 ms |
49904 KB |
Output is correct |
19 |
Correct |
291 ms |
49400 KB |
Output is correct |
20 |
Correct |
305 ms |
50852 KB |
Output is correct |
21 |
Correct |
296 ms |
50660 KB |
Output is correct |
22 |
Correct |
696 ms |
37740 KB |
Output is correct |
23 |
Correct |
686 ms |
37616 KB |
Output is correct |
24 |
Correct |
683 ms |
37496 KB |
Output is correct |
25 |
Correct |
697 ms |
37620 KB |
Output is correct |
26 |
Correct |
345 ms |
41376 KB |
Output is correct |
27 |
Correct |
348 ms |
39544 KB |
Output is correct |
28 |
Correct |
353 ms |
38520 KB |
Output is correct |
29 |
Correct |
297 ms |
50764 KB |
Output is correct |
30 |
Correct |
290 ms |
50660 KB |
Output is correct |
31 |
Correct |
115 ms |
39780 KB |
Output is correct |
32 |
Correct |
295 ms |
50664 KB |
Output is correct |