#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 100005
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 |
6 ms |
2808 KB |
Output is correct |
2 |
Correct |
7 ms |
2808 KB |
Output is correct |
3 |
Correct |
7 ms |
2808 KB |
Output is correct |
4 |
Correct |
6 ms |
2808 KB |
Output is correct |
5 |
Correct |
7 ms |
2808 KB |
Output is correct |
6 |
Correct |
7 ms |
2808 KB |
Output is correct |
7 |
Correct |
7 ms |
2808 KB |
Output is correct |
8 |
Correct |
7 ms |
2808 KB |
Output is correct |
9 |
Correct |
7 ms |
2936 KB |
Output is correct |
10 |
Correct |
7 ms |
2784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
9 ms |
5368 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2808 KB |
Output is correct |
2 |
Correct |
7 ms |
2808 KB |
Output is correct |
3 |
Correct |
7 ms |
2808 KB |
Output is correct |
4 |
Correct |
6 ms |
2808 KB |
Output is correct |
5 |
Correct |
7 ms |
2808 KB |
Output is correct |
6 |
Correct |
7 ms |
2808 KB |
Output is correct |
7 |
Correct |
7 ms |
2808 KB |
Output is correct |
8 |
Correct |
7 ms |
2808 KB |
Output is correct |
9 |
Correct |
7 ms |
2936 KB |
Output is correct |
10 |
Correct |
7 ms |
2784 KB |
Output is correct |
11 |
Runtime error |
9 ms |
5368 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |