#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n;
ll a[300009];
vector<ll> g[300009];
ll sz[300009];
ll block[300009];
ll ans;
void findsize(ll v, ll p){
sz[v] = 1;
for(auto u : g[v]){
if(u == p || block[u])
continue;
findsize(u, v);
sz[v] += sz[u];
}
}
ll findcentroid(ll v, ll p, ll tot){
for(auto u : g[v]){
if(u == p || block[u])
continue;
if(sz[u] >= tot/2)
return findcentroid(u, v, tot);
}
return v;
}
map<ll, ll> inc[300009];
map<ll, ll> out[300009];
void seccalc(ll v, ll p){
out[v].clear();
inc[v].clear();
if(a[v] == -1)
out[v][a[v]] = 1;
if(a[v] == 1)
inc[v][a[v]] = 1;
for(auto u : g[v]){
if(u == p || block[u])
continue;
seccalc(u, v);
for(auto k : out[u]){
if(a[v]+k.ff <= 0)
out[v][a[v]+k.ff] += k.ss;
}
for(auto k : inc[u]){
if(a[v]+k.ff >= 0)
inc[v][a[v]+k.ff] += k.ss;
}
}
}
void maincalc(ll v){
findsize(v, -1);
v = findcentroid(v, -1, sz[v]);
for(auto u : g[v]){
if(block[u])
continue;
seccalc(u, v);
//cout << v << " "<< u << " ==>\n";
for(auto k : out[u]){
if(a[v]+k.ff > 0)
continue;
//cout << k.ff << " " << k.ss << "\n";
if(a[v]+k.ff == 0)
ans += k.ss;
ans += inc[v][-(a[v]+k.ff)]*k.ss;
}
for(auto k : inc[u]){
if(a[v]+k.ff < 0)
continue;
//cout << k.ff << " " << k.ss << "\n";
if(a[v]+k.ff == 0)
ans += k.ss;
ans += out[v][-(a[v]+k.ff)]*k.ss;
}
for(auto k : out[u]){
if(k.ff <= 0)
out[v][k.ff] += k.ss;
}
for(auto k : inc[u]){
if(k.ff >= 0)
inc[v][k.ff] += k.ss;
}
}
inc[v].clear();
out[v].clear();
block[v] = 1;
for(auto u : g[v]){
if(!block[u])
maincalc(u);
}
}
ll input(){
char c;
cin >> c;
if(c == '(')
return 1;
if(c == ')')
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(ll i = 1; i <= n; ++i)
a[i] = input();
for(ll i = 1; i < n; ++i){
ll t1, t2;
cin >> t1 >> t2;
g[t1].pb(t2);
g[t2].pb(t1);
}
maincalc(1);
cout << ans << "\n";
}
Compilation message
zagrade.cpp: In function 'll input()':
zagrade.cpp:116:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
35832 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2787 ms |
1048576 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
35832 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |