#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7412 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7436 KB |
Output is correct |
4 |
Correct |
5 ms |
7388 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7396 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
6 ms |
7380 KB |
Output is correct |
10 |
Correct |
6 ms |
7384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
577 ms |
47380 KB |
Output is correct |
2 |
Correct |
540 ms |
47680 KB |
Output is correct |
3 |
Correct |
516 ms |
47376 KB |
Output is correct |
4 |
Correct |
546 ms |
47556 KB |
Output is correct |
5 |
Correct |
528 ms |
47352 KB |
Output is correct |
6 |
Correct |
529 ms |
47460 KB |
Output is correct |
7 |
Correct |
524 ms |
47332 KB |
Output is correct |
8 |
Correct |
534 ms |
47588 KB |
Output is correct |
9 |
Correct |
527 ms |
47380 KB |
Output is correct |
10 |
Correct |
556 ms |
47996 KB |
Output is correct |
11 |
Correct |
491 ms |
47372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7412 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
5 ms |
7436 KB |
Output is correct |
4 |
Correct |
5 ms |
7388 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7396 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
6 ms |
7380 KB |
Output is correct |
10 |
Correct |
6 ms |
7384 KB |
Output is correct |
11 |
Correct |
577 ms |
47380 KB |
Output is correct |
12 |
Correct |
540 ms |
47680 KB |
Output is correct |
13 |
Correct |
516 ms |
47376 KB |
Output is correct |
14 |
Correct |
546 ms |
47556 KB |
Output is correct |
15 |
Correct |
528 ms |
47352 KB |
Output is correct |
16 |
Correct |
529 ms |
47460 KB |
Output is correct |
17 |
Correct |
524 ms |
47332 KB |
Output is correct |
18 |
Correct |
534 ms |
47588 KB |
Output is correct |
19 |
Correct |
527 ms |
47380 KB |
Output is correct |
20 |
Correct |
556 ms |
47996 KB |
Output is correct |
21 |
Correct |
491 ms |
47372 KB |
Output is correct |
22 |
Correct |
1220 ms |
24072 KB |
Output is correct |
23 |
Correct |
1095 ms |
24080 KB |
Output is correct |
24 |
Correct |
1147 ms |
24080 KB |
Output is correct |
25 |
Correct |
1207 ms |
24076 KB |
Output is correct |
26 |
Correct |
692 ms |
31712 KB |
Output is correct |
27 |
Correct |
696 ms |
28280 KB |
Output is correct |
28 |
Correct |
702 ms |
26964 KB |
Output is correct |
29 |
Correct |
519 ms |
47860 KB |
Output is correct |
30 |
Correct |
502 ms |
47908 KB |
Output is correct |
31 |
Correct |
85 ms |
23692 KB |
Output is correct |
32 |
Correct |
534 ms |
47384 KB |
Output is correct |