This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define all(s) s.begin(), s.end()
#define ok puts("ok")
#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define pi pair < int, int >
#define pii pair < int, pi >
#define next next123
const int N = 3e5 + 7;
const int INF = 1e9 + 7;
pi st[N], fin[N];
int d[N], sub[N];
int n, t;
ll ans;
string s;
vi g[N];
void dfs (int v, int p = 0){
sub[v] = 1;
for (int to : g[v]){
if (!d[to] && to != p){
dfs(to, v);
sub[v] += sub[to];
}
}
}
int fc (int v){
bool good = true;
int sz = sub[v] / 2, p = 0;
while (good){
good = false;
for (int to : g[v]){
if (!d[to] && to != p && sub[to] > sz){
good = true;
p = v; v = to;
break;
}
}
}
return v;
}
vi nw;
void calc (int v, int p, int ad = 0, int bal = 0, int mnb = 0, int mxb = 0){
if (s[v] == '(')
bal++;
else
bal--;
mxb = max(mxb, bal);
mnb = min(mnb, bal);
if (mxb == bal){
if (bal){
nw.pb(bal);
if (mxb == bal){
if (fin[bal + ad - (!ad)].sc != t)
fin[bal + ad - (!ad)] = {0, t};
ans += fin[bal + ad - (!ad)].fr;
}
}
else {
nw.pb(INF);
if (mxb == bal && ad){
if (fin[bal + 1].sc != t)
fin[bal + 1] = {0, t};
ans += fin[bal + 1].fr;
}
}
}
if (mnb == bal){
if (bal){
nw.pb(bal);
if (mnb == bal){
if (st[-bal + (!ad) - ad].sc != t)
st[-bal + (!ad) - ad] = {0, t};
ans += st[-bal + (!ad) - ad].fr;
}
}
else {
nw.pb(-INF);
if (mnb == bal && !ad){
if (st[bal + 1].sc != t)
st[bal + 1] = {0, t};
ans += st[bal + 1].fr;
}
}
}
for (int to : g[v])
if (!d[to] && to != p)
calc(to, v, ad, bal, mnb, mxb);
}
void cd (int v = 1){
dfs(v);
v = fc(v);
fin[0] = {1, t};
st[0] = {1, t};
d[v] = 1;
for (int to : g[v]){
if (!d[to]){
nw.clear();
calc(to, v, (s[v] == '('));
for (int it : nw){
if (it == -INF){
if (fin[0].sc != t)
fin[0] = {0, t};
fin[0].fr++;
}
else if (it == INF){
if (st[0].sc != t)
st[0] = {0, t};
st[0].fr++;
}
else if (it < 0){
if (fin[-it].sc != t)
fin[-it] = {0, t};
fin[-it].fr++;
}
else {
if (st[it].sc != t)
st[it] = {0, t};
st[it].fr++;
}
}
}
}
++t;
for (int to : g[v])
if (!d[to])
cd(to);
}
main(){
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i < n; i++){
int a, b;
scanf("%d%d", &a, &b);
g[a].pb(b);
g[b].pb(a);
}
cd();
cout << ans;
}
Compilation message (stderr)
zagrade.cpp:146:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
zagrade.cpp: In function 'int main()':
zagrade.cpp:151:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |