# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
250860 |
2020-07-19T10:10:39 Z |
VEGAnn |
Zagrade (COI17_zagrade) |
C++14 |
|
527 ms |
43308 KB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define PB push_back
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
const int N = 300100;
gp_hash_table<int, int> mem;
vector<int> g[N];
ll ans;
int n, siz[N], ad[N], sta[N], ed[N], mn_sta[N], mn_ed[N];
bool used[N];
string s;
void build_sz(int v, int p){
siz[v] = 1;
for (int u : g[v]){
if (u == p || used[v]) continue;
build_sz(u, v);
siz[v] += siz[u];
}
}
int search(int v, int pr, int SZ){
for (int u : g[v]){
if (u == pr || used[v]) continue;
if (siz[u] > SZ / 2)
return search(u, v, SZ);
}
return v;
}
void dfs(int v, int pr){
mn_sta[v] = min(mn_sta[pr] + ad[v], 0);
mn_ed[v] = min(mn_ed[pr] - ad[v], 0);
sta[v] = sta[pr] + ad[v];
ed[v] = ed[pr] + ad[v];
for (int u : g[v]){
if (used[u] || u == pr) continue;
dfs(u, v);
}
}
void calc(int v, int pr){
if (mn_sta[v] == 0 && mem.find(-sta[v]) != mem.end())
ans += mem[-sta[v]];
for (int u : g[v]){
if (used[u] || u == pr) continue;
calc(u, v);
}
}
void upda(int v, int pr){
if (mn_ed[v] == 0)
mem[ed[v]]++;
for (int u : g[v]){
if (used[u] || u == pr) continue;
upda(u, v);
}
}
void build_cd(int v){
build_sz(v, -1);
int cen = search(v, -1, siz[v]);
used[cen] = 1;
// calc subtree sums
sta[cen] = 0;
ed[cen] = ad[cen];
mn_ed[cen] = min(0, -ad[cen]);
for (int u : g[cen]){
if (used[u]) continue;
dfs(u, cen);
}
// update ans (left, right)
//left
mem.clear();
if (mn_ed[cen] == 0)
mem[ed[cen]]++;
for (int u : g[cen]){
if (used[u]) continue;
calc(u, cen);
upda(u, cen);
}
//right
mem.clear();
reverse(all(g[cen]));
for (int u : g[cen]){
if (used[u]) continue;
calc(u, cen);
upda(u, cen);
}
if (mn_sta[cen] == 0)
ans += mem[-sta[cen]];
// continue process in children subtrees
for (int u : g[cen])
if (!used[u])
build_cd(u);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> s;
for (int i = 0; i < n; i++){
if (s[i] == '(')
ad[i] = 1;
else ad[i] = -1;
}
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
x--; y--;
g[x].PB(y);
g[y].PB(x);
}
build_cd(0);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
527 ms |
43308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
7424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |