#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 10007;
const ll LOGN = 20;
const ll MAXN = 1e4 + 100;
vector<vector<int>> graph;
vector<pair<int,int>> possible[MAXN];
vector<int> border[105];
bitset<105> bs[MAXN][105];
int V[MAXN];
void dfs(int node) {
for (auto u : graph[node])
dfs(u);
for (int i = 0; i < 105; i++)
border[i].clear();
for (auto u : graph[node]) {
for (auto q : possible[u])
border[q.f].push_back(q.s);
}
for (int i = 104; i >= 1; i--) {
if (i == V[node]) {
bs[node][i] |= bs[node][i+1];
bs[node][i].set(i);
} else {
for (auto u : border[i]) {
if (u < V[node] || i > V[node]) {
bs[node][i] |= bs[node][u + 1];
bs[node][i].set(u);
}
}
}
for (int r = i; r <= 104; r++) {
if (bs[node][i].test(r) && V[node] >= i && V[node] <= r)
possible[node].push_back({i, r});
}
}
}
int main() {
fast
int N, A, B;
cin >> N;
for (int i = 1; i <= N; i++)
cin >> V[i];
graph = vector<vector<int>>(N+1, vector<int>());
for (int i = 1; i < N; i++) {
cin >> A >> B;
graph[A].push_back(B);
}
dfs(1);
cout << possible[1].size() << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
744 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
788 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
54 ms |
17792 KB |
Output is correct |
12 |
Correct |
54 ms |
17744 KB |
Output is correct |
13 |
Correct |
53 ms |
17908 KB |
Output is correct |
14 |
Correct |
62 ms |
24304 KB |
Output is correct |
15 |
Correct |
69 ms |
24324 KB |
Output is correct |
16 |
Correct |
66 ms |
24504 KB |
Output is correct |
17 |
Correct |
54 ms |
17748 KB |
Output is correct |
18 |
Correct |
57 ms |
17744 KB |
Output is correct |
19 |
Correct |
55 ms |
20048 KB |
Output is correct |
20 |
Correct |
61 ms |
20052 KB |
Output is correct |