This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef CPL_TEMPLATE
#define CPL_TEMPLATE
/*
Normie's Template v2.5
Changes:
Added warning against using pragmas on USACO.
*/
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
//Pragmas (Comment out these three lines if you're submitting in szkopul or USACO.)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast,unroll-loops,tree-vectorize")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0)
#define nfio cin.tie(0)
#define endl "\n"
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
// Fast min/max assigns to use with AVX.
// Requires g++ 9.2.0.
template<typename T>
__attribute__((always_inline)) void chkmin(T& a, const T& b) {
a=(a<b)?a:b;
}
template<typename T>
__attribute__((always_inline)) void chkmax(T& a, const T& b) {
a=(a>b)?a:b;
}
//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
const long double PI=3.14159265358979;
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
// Binpow and stuff
ll BOW(ll a, ll x, ll p)
{
if (!x) return 1;
ll res=BOW(a,x/2,p);
res*=res;
res%=p;
if (x%2) res*=a;
return res%p;
}
ll INV(ll a, ll p)
{
return BOW(a,p-2,p);
}
//---------END-------//
#endif
set<int> gt[300001];
int n,m,i,j,k,t,t1,u,v,a,b,glob;
ll res;
string s;
int l[300001];
int r[300001];
int sz[300001];
int arr[300001];
map<int,int> mpr,mpl;
void prepare(int x, int p) {
sz[x]=1;
for (auto g : gt[x]) if (g-p) {
prepare(g,x);
sz[x]+=sz[g];
}
}
void init(int x, int p) {
if (x==p) {
if (s[x-1]=='(') arr[x]=1;
else arr[x]=-1;
l[x]=min(0,arr[x]);
r[x]=max(0,arr[x]);
}
else {
if (s[x-1]=='(') arr[x]=arr[p]+1;
else arr[x]=arr[p]-1;
l[x]=min(l[p],arr[x]);
r[x]=max(r[p],arr[x]);
}
if (arr[x]==0 && arr[x]==l[x]) res++;
if (arr[x]==0 && arr[x]==r[x]) res++;
for (auto g : gt[x]) if (g-p) init(g,x);
}
void calc(int x, int p) {
if (l[x]==arr[x]) res+=mpr[glob-arr[x]];
if (r[x]==arr[x]) res+=mpl[glob-arr[x]];
for (auto g : gt[x]) if (g-p) calc(g,x);
}
void add(int x, int p) {
if (l[x]==arr[x]) mpl[arr[x]]++;
if (r[x]==arr[x]) mpr[arr[x]]++;
for (auto g : gt[x]) if (g-p) add(g,x);
}
void solve(int x) {
prepare(x,x);
int rt=x,p=x,a=0;
while(true) {
a=0;
for (auto g : gt[rt]) if (g-p && sz[g]>sz[a]) a=g;
if (sz[a]*2<=sz[x] && sz[rt]*2>=sz[x]) break;
else {
p=rt;
rt=a;
}
}
init(rt,rt);
mpr.clear();
mpl.clear();
glob=arr[rt];
for (auto g : gt[rt]) {
calc(g,rt);
add(g,rt);
}
for (auto g : gt[rt]) {
gt[g].erase(rt);
solve(g);
}
}
int main()
{
fio;
cin>>n;
cin>>s;
for (i=1;i<n;i++) {
cin>>u>>v;
gt[u].insert(v);
gt[v].insert(u);
}
solve(1);
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |