#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1e4)+2, MAXK = 102;
int n, a[MAXN];
vector<int> g[MAXN], q[MAXK];
vector<pair<int,int> > s[MAXN];
bitset<MAXK> dp[MAXN][MAXK];
void dfs(int u) {
for(int v: g[u]) dfs(v);
for(int i = 1; i < MAXK; i++) q[i].clear();
for(int v: g[u]) {
for(auto p: s[v]) q[p.first].push_back(p.second);
}
for(int l = MAXK-1; l >= 1; l--) {
if(l == a[u]) {
dp[u][l] |= dp[u][l+1];
dp[u][l][l] = true;
} else for(int r: q[l]) {
if(a[u] < l || a[u] > r) {
dp[u][l] |= dp[u][r+1];
dp[u][l][r] = true;
}
}
for(int r = MAXK-1; r >= l; r--) {
if(dp[u][l][r] && a[u] >= l && a[u] <= r) s[u].emplace_back(l, r);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
}
dfs(1);
cout << s[1].size();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
18628 KB |
Output is correct |
2 |
Correct |
0 ms |
18628 KB |
Output is correct |
3 |
Correct |
0 ms |
18628 KB |
Output is correct |
4 |
Correct |
0 ms |
18628 KB |
Output is correct |
5 |
Correct |
0 ms |
18628 KB |
Output is correct |
6 |
Correct |
0 ms |
18628 KB |
Output is correct |
7 |
Correct |
0 ms |
18628 KB |
Output is correct |
8 |
Correct |
6 ms |
18628 KB |
Output is correct |
9 |
Correct |
3 ms |
18628 KB |
Output is correct |
10 |
Correct |
3 ms |
18628 KB |
Output is correct |
11 |
Correct |
179 ms |
19024 KB |
Output is correct |
12 |
Correct |
186 ms |
19024 KB |
Output is correct |
13 |
Correct |
206 ms |
19156 KB |
Output is correct |
14 |
Correct |
206 ms |
25332 KB |
Output is correct |
15 |
Correct |
186 ms |
25336 KB |
Output is correct |
16 |
Correct |
189 ms |
25332 KB |
Output is correct |
17 |
Correct |
183 ms |
19156 KB |
Output is correct |
18 |
Correct |
153 ms |
19156 KB |
Output is correct |
19 |
Correct |
176 ms |
22328 KB |
Output is correct |
20 |
Correct |
186 ms |
22328 KB |
Output is correct |