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 all(s) s.begin(),s.end()
#define F first
#define S second
using namespace std;
typedef int ll;
ll n,x,y,b[1009],ans;
char c;
vector<vector<ll> >v;
void dfs(ll d,ll p,ll sum)
{
if(sum<0)
return;
if(sum==0)
ans++;
for(auto z:v[d])
if(z!=p)
dfs(z,d,sum+b[z]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
v.resize(n+1);
for(ll i=1; i<=n; i++)
{
cin>>c;
if(c=='(')
b[i]=1;
else
b[i]=-1;
}
for(ll i=1; i<n; i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(ll i=1;i<=n;i++)
dfs(i,i,b[i]);
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |