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 st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<ii,int>
#define sz(x) (x.size())
#define orta ((bas+son)>>1)
#define all(x) x.begin(),x.end()
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define pw(x) (1<<(x))
#define inf 1000000000
#define MOD 1000000007
#define N 300005
#define MAX 10000006
#define LOG 20
#define count anan
using namespace std;
int sub[N],F[N],count[N];
int n,x,y,Gsub;
ll ans;
vector<int> v[N];
char s[N];
void dfs_to(int node,int ata,int bal,int prob,int add) {
if(bal==0 && s[node]=='(') prob++;
bal=min(0,bal+(s[node]=='('?1:-1));
if(bal==0) count[prob]+=add;
for(int i:v[node]) {
if(i==ata || F[i]) continue ;
dfs_to(i,node,bal,prob,add);
}
}
void dfs_from(int node,int ata,int bal,int prob) {
if(bal==0 && s[node]==')') prob++;
bal=max(0,bal+(s[node]=='('?1:-1));
if(bal==0) ans+=count[prob];
for(int i:v[node]) {
if(i==ata || F[i]) continue ;
dfs_from(i,node,bal,prob);
}
}
void fsubs(int node,int ata) {
sub[node]=1;
for(int i:v[node]) {
if(i==ata || F[i]) continue ;
fsubs(i,node);
sub[node]+=sub[i];
}
}
int fcnt(int node,int ata) {
for(int i:v[node]) {
if(i==ata || F[i]) continue ;
if(sub[i]>Gsub/2) return fcnt(i,node);
}
return node;
}
void solve(int node) {
fsubs(node,0);
Gsub=sub[node];
int cnt=fcnt(node,0);
F[cnt]=1;
count[0]++;
for(int i:v[cnt]) {
if(F[i]) continue ;
dfs_from(i,cnt,s[cnt]=='(',s[cnt]==')');
dfs_to(i,cnt,0,0,1);
}
if(s[cnt]==')') ans+=count[1];
for(int i:v[cnt]) {
if(F[i]) continue ;
dfs_to(i,cnt,0,0,-1);
}
// printf("PHCNT %d A %d\n",cnt,ans);
reverse(all(v[cnt]));
count[0]--;
for(int i:v[cnt]) {
if(F[i]) continue ;
dfs_from(i,cnt,s[cnt]=='(',s[cnt]==')');
dfs_to(i,cnt,0,0,1);
}
for(int i:v[cnt]) {
if(F[i]) continue ;
dfs_to(i,cnt,0,0,-1);
}
// printf("C%d A%d\n",cnt,ans);
for(int i:v[cnt]) {
if(F[i]) continue ;
solve(i);
}
}
int main() {
// freopen("input.txt","r",stdin);
scanf("%d",&n);
scanf("%s",s+1);
for(int i=1;i<n;i++) {
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
solve(1);
printf("%lld",ans);
}
Compilation message (stderr)
zagrade.cpp: In function 'int main()':
zagrade.cpp:169:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
zagrade.cpp:171:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s+1);
~~~~~^~~~~~~~~~
zagrade.cpp:175:8: 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |