#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 1e4 + 7;
int n, u, v, a[N];
bool vis[2][N], l[101][N], r[101][N];
long long ans = 0, bns = 0;
vector <int> edge[N];
void dfs(int u){
for(int v : edge[u]) dfs(v);
l[a[u]][u] = r[a[u]][u] = 1;
for(int i = a[u]; i > 1; i--){
if(!l[i][u]) continue;
for(int v : edge[u]){
if(vis[0][v] || !r[i - 1][v]) continue;
vis[0][v] = true;
for(int j = 1; j <= 100; j++) l[j][u] = max(l[j][u], l[j][v]);
}
}
for(int i = a[u]; i < 100; i++){
if(!r[i][u]) continue;
for(int v : edge[u]){
if(vis[1][v] || !l[i + 1][v]) continue;
vis[1][v] = true;
for(int j = 1; j <= 100; j++) r[j][u] = max(r[j][u], r[j][v]);
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i < n; i++){
cin >> u >> v;
edge[u].pb(v);
}
dfs(1);
for(int i = 1; i <= 100; i++) ans += l[i][1];
for(int i = 1; i <= 100; i++) bns += r[i][1];
cout << ans * bns;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1408 KB |
Output is correct |
2 |
Correct |
3 ms |
1408 KB |
Output is correct |
3 |
Correct |
3 ms |
1408 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
4 ms |
1408 KB |
Output is correct |
6 |
Correct |
4 ms |
1408 KB |
Output is correct |
7 |
Correct |
3 ms |
1324 KB |
Output is correct |
8 |
Correct |
3 ms |
1408 KB |
Output is correct |
9 |
Correct |
4 ms |
1276 KB |
Output is correct |
10 |
Correct |
3 ms |
1352 KB |
Output is correct |
11 |
Correct |
13 ms |
2800 KB |
Output is correct |
12 |
Correct |
16 ms |
2688 KB |
Output is correct |
13 |
Correct |
15 ms |
2808 KB |
Output is correct |
14 |
Correct |
14 ms |
3552 KB |
Output is correct |
15 |
Correct |
18 ms |
3428 KB |
Output is correct |
16 |
Correct |
20 ms |
3432 KB |
Output is correct |
17 |
Correct |
18 ms |
2788 KB |
Output is correct |
18 |
Correct |
16 ms |
2808 KB |
Output is correct |
19 |
Correct |
17 ms |
2708 KB |
Output is correct |
20 |
Correct |
17 ms |
2700 KB |
Output is correct |