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>
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
using namespace std;
using ll = long long;
using pii = pair <int, int>;
const int maxN = 3e5 + 100;
const int inf = 0x3f3f3f3f;
//const int Mod =
string s;
int n;
bool visited[maxN];
int sz[maxN];
int cnt[maxN];
vector <int> adj[maxN];
int a[maxN];
void pre_dfs(int u, int p){
sz[u] = 1;
for (int i: adj[u]){
if (i == p || visited[i]) continue;
pre_dfs(i, u);
sz[u] += sz[i];
}
}
int find_cent(int u, int p, int szz){
for (int i: adj[u]){
if (i == p || visited[i]) continue;
if (sz[i] > szz / 2) return find_cent(i, u, szz);
}
return u;
}
ll ans = 0;
void dfs(int u, int p, int t, int d, int minn){
//t == 0: calculate
//t == 1: add
//t == 2: delete
if (t == 0){
minn = min(minn, d);
//if (u == 4 && p == 2) cerr << d << " " << minn << "\n";
if (minn == d){
ans += cnt[-minn];
}
}
if (t == 1){
minn = min(minn + a[u], a[u]);
if (minn >= 0) ++cnt[d];
}
if (t == 2){
minn = min(minn + a[u], a[u]);
if (minn >= 0) --cnt[d];
}
for (int i: adj[u]){
if (i == p || visited[i]) continue;
dfs(i, u, t, d + a[i], minn);
}
}
void update(int u){
//if (u == 2) cerr << a[4] << ":D";
if (a[u] == 1) cnt[1] = 1;
for (int i: adj[u]){
if (visited[i]) continue;
dfs(i, u, 0, a[i], 0);
dfs(i, u, 1, a[u] + a[i], a[u]);
}
for (int i: adj[u]){
if (visited[i]) continue;
dfs(i, u, 2, a[u] + a[i], a[u]);
}
if (a[u] == 1) cnt[1] = 0;
}
void build_cent(int u){
pre_dfs(u, u);
int cent = find_cent(u, u, sz[u]);
//cerr << cent << "\n";
visited[cent] = 1;
update(cent);
for (int i: adj[cent]){
if (visited[i]) continue;
build_cent(i);
}
}
void Init(){
cin >> n;
cin >> s;
s = " " + s;
for (int i = 1; i <= n; ++i){
if (s[i] == '(') a[i] = 1;
else a[i] = -1;
}
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
build_cent(1);
fill(visited + 1, visited + 1 + n, 0);
for (int i = 1; i <= n; ++i) a[i] = -a[i];
build_cent(1);
cout << ans;
}
#define taskname "test"
signed main(){
faster
if (fopen(taskname ".inp", "r")){
freopen(taskname ".inp", "r", stdin);
freopen(taskname ".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
}
}
Compilation message (stderr)
zagrade.cpp: In function 'int main()':
zagrade.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(taskname ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zagrade.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(taskname ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |