# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
40173 | krauch | Zagrade (COI17_zagrade) | C++14 | 0 ms | 0 KiB |
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>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair < ll, int > PLI;
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define right(x) x << 1 | 1
#define left(x) x << 1
#define forn(x, a, b) for (int x = a; x <= b; ++x)
#define for1(x, a, b) for (int x = a; x >= b; --x)
#define mkp make_pair
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define y1 kekekek
#define fname ""
const ll ool = 1e18 + 9;
const int oo = 1e9 + 9, base = 1e9 + 7;
const ld eps = 1e-7;
const int N = 5e5 + 6;
int n, ROOT = 0, cnt[N], tmp[N];
bool u[N];
char a[N];
vector < int > g[N];
ll ans;
int getsz(int v, int par, int sz) {
int res = 1;
bool ok = 1;
for (auto to : g[v]) {
if (to == par || u[to]) continue;
int val = getsz(to, v, sz);
if (val > sz / 2) ok = 0;
res += val;
}
if (sz - res > sz / 2) ok = 0;
if (ok) ROOT = v;
return res;
}
void dfs(int v, int par, int bal, int mn) {
bal += (a[v] == ')' ? -1 : 1);
mn = min(mn, bal);
if (bal == mn) cnt[-bal]++;
for (auto to : g[v]) {
if (to == par || u[to]) continue;
dfs(to, v, bal, mn);
}
}
void calc(int v, int par, int bal, int mn, int bal2, int mx, int kek) {
bal += (a[v] == ')' ? -1 : 1);
bal2 += (a[v] == ')' ? -1 : 1);
mn = min(mn, bal);
mx = max(mx, bal2);
if (bal == mn) cnt[-bal] -= kek;
if (bal == mn) MXVAL = max(MXVAL, abs(bal));
if (bal2 == mx) tmp[bal2] += kek;
if (bal2 == mx) MXVAL = max(MXVAL, abs(bal2));
for (auto to : g[v]) {
if (to == par || u[to]) continue;
calc(to, v, bal, mn, bal2, mx, kek);
}
}
void build(int v, int sz) {
getsz(v, 0, sz);
int root = ROOT;
//cerr << root << " " << sz << "\n";
forn(i, 0, sz / 2 + 1) cnt[i] = 0;
dfs(root, 0, 0, 0);
for (auto to : g[root]) {
if (u[to]) continue;
MXVAL = 0;
calc(to, root, (a[root] == ')' ? -1 : 1), (a[root] == ')' ? -1 : 1), 0, 0, 1);
forn(i, 0, MXVAL) {
ans += 1ll * cnt[i] * tmp[i];
//cerr << root << " " << to << " " << i << " " << cnt[i] << " " << tmp[i] << "\n";
}
calc(to, root, (a[root] == ')' ? -1 : 1), (a[root] == ')' ? -1 : 1), 0, 0, -1);
}
ans += cnt[0];
//cerr << "kek " << cnt[0] << "\n";
u[root] = 1;
vector < int > vec;
for (auto to : g[root]) {
vec.eb(0);
if (u[to]) continue;
vec.back() = getsz(to, root, sz);
}
forn(i, 0, sz(g[root]) - 1) {
if (u[g[root][i]]) continue;
build(g[root][i], vec[i]);
}
}
int main() {
//ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef krauch
freopen("input.txt", "r", stdin);
#else
//freopen(fname".in", "r", stdin);
//freopen(fname".out", "w", stdout);
#endif
scanf("%d\n", &n);
forn(i, 1, n) {
scanf("%c", a + i);
}
scanf("\n");
forn(i, 1, n - 1) {
int x, y;
scanf("%d %d\n", &x, &y);
g[x].eb(y);
g[y].eb(x);
}
build(1, n);
cout << ans << "\n";
return 0;
}