#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n;
int a[300009];
vector<int> g[300009];
int ans;
unordered_map<int, int> inc[300009];
unordered_map<int, int> out[300009];
void calc(int v, int p){
if(a[v] < 0)
out[v][a[v]] = 1;
else
inc[v][a[v]] = 1;
for(auto u : g[v]){
if(u == p)
continue;
calc(u, v);
for(auto k : out[u]){
if(k.ff > 0)
continue;
ans += inc[v][-k.ff]*k.ss;
if(inc[v][-k.ff] == 0)
inc[v].erase(-k.ff);
}
for(auto k : inc[u]){
if(k.ff < 0)
continue;
ans += out[v][-k.ff]*k.ss;
if(out[v][-k.ff] == 0)
out[v].erase(-k.ff);
}
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;
}
inc[u].clear();
out[u].clear();
}
}
int 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(int i = 1; i <= n; ++i)
a[i] = input();
for(int i = 1; i < n; ++i){
int t1, t2;
cin >> t1 >> t2;
g[t1].pb(t2);
g[t2].pb(t1);
}
calc(1, -1);
cout << ans << "\n";
}
Compilation message
zagrade.cpp: In function 'int input()':
zagrade.cpp:65:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
40312 KB |
Output is correct |
2 |
Correct |
43 ms |
40316 KB |
Output is correct |
3 |
Correct |
44 ms |
40312 KB |
Output is correct |
4 |
Correct |
42 ms |
40316 KB |
Output is correct |
5 |
Correct |
42 ms |
40312 KB |
Output is correct |
6 |
Correct |
43 ms |
40312 KB |
Output is correct |
7 |
Correct |
42 ms |
40440 KB |
Output is correct |
8 |
Correct |
42 ms |
40312 KB |
Output is correct |
9 |
Correct |
43 ms |
40312 KB |
Output is correct |
10 |
Correct |
43 ms |
40312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3115 ms |
224220 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
40312 KB |
Output is correct |
2 |
Correct |
43 ms |
40316 KB |
Output is correct |
3 |
Correct |
44 ms |
40312 KB |
Output is correct |
4 |
Correct |
42 ms |
40316 KB |
Output is correct |
5 |
Correct |
42 ms |
40312 KB |
Output is correct |
6 |
Correct |
43 ms |
40312 KB |
Output is correct |
7 |
Correct |
42 ms |
40440 KB |
Output is correct |
8 |
Correct |
42 ms |
40312 KB |
Output is correct |
9 |
Correct |
43 ms |
40312 KB |
Output is correct |
10 |
Correct |
43 ms |
40312 KB |
Output is correct |
11 |
Execution timed out |
3115 ms |
224220 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |