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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 300005;
int n;
string s;
vector<int> g[mxN];
int cnt[mxN];
bool bio[mxN];
void dfs_cnt(int v, int p){
cnt[v] = 1;
for(auto u : g[v]){
if(u != p && !bio[u]){
dfs_cnt(u, v);
cnt[v] += cnt[u];
}
}
}
int centroid(int v, int p, int vel){
for(auto u : g[v]){
if(u == p || bio[u])continue;
if(cnt[v] > vel / 2)return centroid(u, v, vel);
}
return v;
}
ll br = 0;
vector<int> cuvaj;
map<int,int> kol;
int X = 0; bool f = 0;
void dfs(int v, int p, int sumA, int dlA, int grB, int X, bool f){
int Y = (s[v] == '(' ? 1 : -1);
sumA += Y; dlA = min(dlA, sumA); grB = min(0, grB + Y);
if(dlA >= sumA && kol.count(sumA) == 1)br += kol[sumA];
if(f == 1){
if(sumA + X == 0 && min(0, dlA + X) >= 0)br += 1;
if(sumA + X == 0 && grB >= 0)br += 1;
}
if(grB == 0)cuvaj.pb(-(sumA + X));
for(auto u : g[v]){
if(u != p && !bio[u]){
dfs(u, v, sumA, dlA, grB, X, f);
}
}
}
void decompose(int v, int p){
dfs_cnt(v, -1); int cen = centroid(v, -1, cnt[v]);
bio[cen] = 1; int X = (s[cen] == '(' ? 1 : -1);
kol.clear();
for(auto u : g[cen]){
if(u != p && !bio[u]){
dfs(u, cen, 0, 0, min(0, X), X, 1);
for(auto c : cuvaj)kol[c] += 1;
cuvaj.clear();
}
}
reverse(all(g[cen]));
kol.clear();
for(auto u : g[cen]){
if(u != p && !bio[u]){
dfs(u, cen, 0, 0, min(0, X), X, 0);
for(auto c : cuvaj)kol[c] += 1;
cuvaj.clear();
}
}
// reverse(all(g[cen]));
for(auto u : g[cen]){
if(u != p && !bio[u]){
decompose(u, cen);
}
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
s = ' ' + s;
ff(i,1,n - 1){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
decompose(1, -1);
cout << br << '\n';
return 0;
}
/*
// probati bojenje sahovski
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |