#include <bits/stdc++.h>
#define ii pair<int,int>
#define fi first
#define se second
using namespace std;
int n, v[10005];
vector<int> V[10005],q[105];
vector<ii> s[10005];
bitset<105> f[10005][105];
void Dfs(int x)
{
for(int y : V[x]) Dfs(y);
for(int i = 0; i <= 100; i++) q[i].clear();
for(int y : V[x])
{
for(ii it : s[y])
q[it.fi].push_back(it.se);
}
for(int l = 100; l >= 1; l--)
{
if (l == v[x])
{
f[x][l] |= f[x][l + 1];
f[x][l].set(l);
}
else
{
for(int r : q[l])
if (r < v[x] || l > v[x])
{
f[x][l] |= f[x][r + 1];
f[x][l].set(r);
}
}
for(int r = 100; r >= l; r--) if(f[x][l][r] && v[x] >= l && v[x] <= r) s[x].push_back({l, r});
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++) cin >> v[i];
for(int i = 1; i < n; i++)
{
int a,b;
cin >> a >> b;
a--;
b--;
V[a].push_back(b);
}
Dfs(0);
cout << s[0].size();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
936 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
1000 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
69 ms |
17740 KB |
Output is correct |
12 |
Correct |
71 ms |
17756 KB |
Output is correct |
13 |
Correct |
69 ms |
17684 KB |
Output is correct |
14 |
Correct |
124 ms |
24412 KB |
Output is correct |
15 |
Correct |
78 ms |
24412 KB |
Output is correct |
16 |
Correct |
79 ms |
24392 KB |
Output is correct |
17 |
Correct |
69 ms |
17656 KB |
Output is correct |
18 |
Correct |
91 ms |
17640 KB |
Output is correct |
19 |
Correct |
73 ms |
20088 KB |
Output is correct |
20 |
Correct |
87 ms |
20092 KB |
Output is correct |