// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define X first
#define Y second
#define ll long long
#define MP make_pair
using namespace std;
const int N = 1e4 + 1, M = 100;
const ll mod = 1e9 + 7;
int n, cl[N];
vector<int> g[N];
vector< pair<int, int> > ans[N];
bitset<M> dp[M], tmp;
void dfs(int v) {
for(int to: g[v])
dfs(to);
for(int i = M - 1;i >= 0;i--)
dp[i].reset();
for(int to: g[v]) {
for(pair<int, int> &nx: ans[to]) {
if(nx.X <= cl[v] && cl[v] <= nx.Y) continue;
dp[nx.X][nx.Y] = 1;
}
}
dp[cl[v]].reset();
for(int i = M - 1;i >= 0;i--) {
if(i == cl[v]) {
if(i + 1 < M) dp[i] = dp[i + 1];
dp[i][i] = 1;
}
else {
for(int j = M - 1;j >= i;j--) {
if(i <= cl[v] && cl[v] <= j) continue;
if(dp[i][j] && j + 1 < M) {
dp[i] |= dp[j + 1];
}
}
}
if(i > cl[v]) continue;
for(int j = cl[v];j < M;j++)
if(dp[i][j]) ans[v].push_back(MP(i, j));
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)
cin >> cl[i], cl[i]--;
for(int i = 1, x, y;i < n;i++) {
cin >> x >> y;
g[x].push_back(y);
}
dfs(1);
cout << ans[1].size();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
98 ms |
1244 KB |
Output is correct |
12 |
Correct |
104 ms |
1268 KB |
Output is correct |
13 |
Correct |
104 ms |
1264 KB |
Output is correct |
14 |
Correct |
117 ms |
7620 KB |
Output is correct |
15 |
Correct |
116 ms |
7748 KB |
Output is correct |
16 |
Correct |
114 ms |
7620 KB |
Output is correct |
17 |
Correct |
115 ms |
1448 KB |
Output is correct |
18 |
Correct |
101 ms |
1308 KB |
Output is correct |
19 |
Correct |
107 ms |
3524 KB |
Output is correct |
20 |
Correct |
110 ms |
3592 KB |
Output is correct |