#include <bits/stdc++.h>
using namespace std;
#define N 10005
#define M 105
#define se second
#define fi first
#define ii pair<int, int>
int n, v[N];
vector<int> adj[N], rr[N];
vector<ii> cn[N];
bitset<M> gt[N][M];
bool in (int l, int r, int v){
return (v >= l && v <= r);
}
void dfs(int u){
for (auto x : adj[u]){
dfs(x);
}
for (int i = 1; i <= 100; i++){
rr[i].clear();
}
for (auto x : adj[u]){
for (auto pi : cn[x]){
int l = pi.fi;
int r = pi.se;
rr[l].push_back(r);
}
}
for (int i = 100; i >= 1; i--){
if (i == v[u]){
gt[u][i] |= gt[u][i + 1];
gt[u][i].set(i);
}
else {
for (auto j : rr[i]){
if (!in(i, j, v[u])){
gt[u][i] |= gt[u][j + 1];
gt[u][i].set(j);
}
}
}
for (int j = 100; j >= i; j--){
if (gt[u][i][j] && in(i, j, v[u])){
cn[u].push_back({i, j});
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n - 1; i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
}
dfs(1);
cout << cn[1].size();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
3160 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
3164 KB |
Output is correct |
10 |
Correct |
1 ms |
3164 KB |
Output is correct |
11 |
Correct |
67 ms |
18160 KB |
Output is correct |
12 |
Correct |
68 ms |
18156 KB |
Output is correct |
13 |
Correct |
70 ms |
18152 KB |
Output is correct |
14 |
Correct |
76 ms |
24756 KB |
Output is correct |
15 |
Correct |
76 ms |
24660 KB |
Output is correct |
16 |
Correct |
74 ms |
24700 KB |
Output is correct |
17 |
Correct |
67 ms |
18196 KB |
Output is correct |
18 |
Correct |
74 ms |
18200 KB |
Output is correct |
19 |
Correct |
69 ms |
20564 KB |
Output is correct |
20 |
Correct |
77 ms |
20592 KB |
Output is correct |