This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |