// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 3e5 + 7;
ll a [N], sz [N], arr1 [N], arr2 [N], cnt [N], L1, L2, R1, R2, n;
vector <ll> adj [N];
bool hide [N];
long long ans;
ll plant (cl &v, cl &pr = -1) {
sz[v] = 1;
for (auto &u : adj[v]) if (!hide[u] && u != pr) sz[v] += plant(u, v);
return sz[v];
}
inline ll find_centroid (ll v, cl &_n, ll pr = -1) {
for (ll i = 0; i < SZ(adj[v]); i++) if (sz[adj[v][i]] << 1 > _n && !hide[adj[v][i]] && adj[v][i] != pr)
pr = v, v = adj[v][i], i = -1;
return v;
}
void dfs (ll v, ll pr, ll df = 0, ll psf = 0, ll nsf = 0) {
df += a[v];
if (a[v] < 0) nsf--;
else if (nsf < 0) nsf++;
if (a[v] > 0) psf++;
else if (psf > 0) psf--;
if (!nsf) arr1[R1++] = abs(df);
if (!psf) arr2[R2++] = abs(df);
for (auto &u : adj[v]) if (!hide[u] && u != pr) dfs(u, v, df, psf, nsf);
}
long long calc (ll v) {
long long ret = 0;
for (ll i = L2; i < R2; i++) cnt[arr2[i]]++;
for (ll i = L1, x; i < R1; i++) {
x = arr1[i] + a[v];
if (x >= 0) ret += cnt[x];
}
for (ll i = L2; i < R2; i++) cnt[arr2[i]]--;
return ret;
}
void centroid_decomposition (ll v = 1) {
v = find_centroid(v, plant(v));
arr1[0] = arr2[0] = 0;
L1 = L2 = R1 = R2 = 1;
for (auto &u : adj[v]) if (!hide[u]) {
dfs(u, v);
ans -= calc(v);
L1 = R1, L2 = R2;
}
L1 = L2 = 0;
ans += calc(v);
hide[v] = 1;
for (auto &u : adj[v]) if (!hide[u]) centroid_decomposition(u);
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n;
for (ll i = 1; i <= n; i++) {
char c; cin>> c;
a[i] = (c == '(' ? +1 : -1);
}
for (ll i = 1, v, u; i < n; i++) {
cin>> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
centroid_decomposition();
cout<< ans << endl;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
7 ms |
7404 KB |
Output is correct |
7 |
Correct |
7 ms |
7424 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
6 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
315 ms |
38252 KB |
Output is correct |
2 |
Correct |
340 ms |
39404 KB |
Output is correct |
3 |
Correct |
327 ms |
38380 KB |
Output is correct |
4 |
Correct |
349 ms |
39020 KB |
Output is correct |
5 |
Correct |
321 ms |
38388 KB |
Output is correct |
6 |
Correct |
320 ms |
38488 KB |
Output is correct |
7 |
Correct |
318 ms |
38252 KB |
Output is correct |
8 |
Correct |
319 ms |
38508 KB |
Output is correct |
9 |
Correct |
324 ms |
38380 KB |
Output is correct |
10 |
Correct |
331 ms |
39916 KB |
Output is correct |
11 |
Correct |
321 ms |
39368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
7 ms |
7404 KB |
Output is correct |
7 |
Correct |
7 ms |
7424 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
6 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
315 ms |
38252 KB |
Output is correct |
12 |
Correct |
340 ms |
39404 KB |
Output is correct |
13 |
Correct |
327 ms |
38380 KB |
Output is correct |
14 |
Correct |
349 ms |
39020 KB |
Output is correct |
15 |
Correct |
321 ms |
38388 KB |
Output is correct |
16 |
Correct |
320 ms |
38488 KB |
Output is correct |
17 |
Correct |
318 ms |
38252 KB |
Output is correct |
18 |
Correct |
319 ms |
38508 KB |
Output is correct |
19 |
Correct |
324 ms |
38380 KB |
Output is correct |
20 |
Correct |
331 ms |
39916 KB |
Output is correct |
21 |
Correct |
321 ms |
39368 KB |
Output is correct |
22 |
Correct |
685 ms |
20356 KB |
Output is correct |
23 |
Correct |
718 ms |
20460 KB |
Output is correct |
24 |
Correct |
683 ms |
20332 KB |
Output is correct |
25 |
Correct |
697 ms |
20440 KB |
Output is correct |
26 |
Correct |
373 ms |
25744 KB |
Output is correct |
27 |
Correct |
355 ms |
23148 KB |
Output is correct |
28 |
Correct |
365 ms |
21868 KB |
Output is correct |
29 |
Correct |
323 ms |
40044 KB |
Output is correct |
30 |
Correct |
346 ms |
40044 KB |
Output is correct |
31 |
Correct |
104 ms |
21728 KB |
Output is correct |
32 |
Correct |
322 ms |
39532 KB |
Output is correct |