//https://oj.uz/problem/view/COCI15_uzastopni
#include<bits/stdc++.h>
const int N = 1e4 + 5;
using namespace std;
vector <int> G[N];
int n, a[N];
bool ck[2][N], l[N][100], r[N][100];
void dfs(int u){
for (auto v : G[u]) dfs(v);
l[u][a[u]] = r[u][a[u]] = true;
for (int i = a[u]; i > 0; i--) if (l[u][i]){
for (auto v : G[u]) if (!ck[0][v] && r[v][i-1]) {
ck[0][v] = true;
for (int i = 0; i < 100; i++) l[u][i] = max(l[u][i], l[v][i]);
}
}
for (int i = a[u]; i < 99; i++) if (r[u][i]){
for (auto v : G[u]) if (!ck[1][v] && l[v][i+1]) {
ck[1][v] = true;
for (int i = 0; i < 100; i++) r[u][i] = max(r[u][i], r[v][i]);
}
}
// cout << u << "\n";
// for (int i = 0; i < 100; i++) if (l[u][i]) cout << i << " "; cout << "\n";
// for (int i = 0; i < 100; i++) if (r[u][i]) cout << i << " "; cout << "\n";
// cout << "\n";
}
int main(){
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], a[i]--;
for (int i = 2; i <= n; i++){
int u, v; cin >> u >> v;
G[u].push_back(v);
}
dfs(1);
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < 100; i++) if (l[1][i]) cnt1++;
for (int i = 0; i < 100; i++) if (r[1][i]) cnt2++;
cout << cnt1 * cnt2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
612 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
18 ms |
2780 KB |
Output is correct |
12 |
Correct |
17 ms |
2820 KB |
Output is correct |
13 |
Correct |
20 ms |
2816 KB |
Output is correct |
14 |
Correct |
16 ms |
3704 KB |
Output is correct |
15 |
Correct |
17 ms |
3704 KB |
Output is correct |
16 |
Correct |
17 ms |
3712 KB |
Output is correct |
17 |
Correct |
17 ms |
2944 KB |
Output is correct |
18 |
Correct |
17 ms |
2816 KB |
Output is correct |
19 |
Correct |
23 ms |
2664 KB |
Output is correct |
20 |
Correct |
19 ms |
2680 KB |
Output is correct |