#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3;
int a[maxn];
vector<int> g[maxn];
int mx[maxn][maxn], dist[maxn][maxn];
void dfsMax(int v, int p, int root) {
for (const int& u : g[v]) {
if (u != p) {
mx[root][u] = max(mx[root][v], a[u]);
dist[root][u] = dist[root][v] + 1;
dfsMax(u, v, root);
}
}
}
void solve() {
int n;
cin >> n;
vector<int> posOf(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
--a[i];
posOf[a[i]] = i;
}
for (int i = 0; i < n - 1; ++i) {
int v, u;
cin >> v >> u;
--v, --u;
g[v].emplace_back(u);
g[u].emplace_back(v);
}
for (int r = 0; r < n; ++r) {
mx[r][r] = a[r];
dist[r][r] = 0;
dfsMax(r, -1, r);
}
vector<long long> dp(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (mx[posOf[i]][posOf[j]] <= i) {
dp[i] = max(dp[i], dp[j] + dist[posOf[i]][posOf[j]]);
}
}
}
cout << dp[n - 1] << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = 1;
cin >> T;
while (T--) {
solve();
cerr << "-----------\n";
cout << "-----------\n";
}
#else
int T = 1;
// cin >> T;
while (T--) {
solve();
}
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
4680 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
4680 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14924 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14924 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
4680 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14924 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14924 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
249 ms |
196784 KB |
Output is correct |
19 |
Correct |
246 ms |
196944 KB |
Output is correct |
20 |
Correct |
247 ms |
196944 KB |
Output is correct |
21 |
Correct |
260 ms |
196848 KB |
Output is correct |
22 |
Correct |
263 ms |
196944 KB |
Output is correct |
23 |
Correct |
263 ms |
196692 KB |
Output is correct |
24 |
Correct |
256 ms |
196948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
4680 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14924 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14924 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
249 ms |
196784 KB |
Output is correct |
19 |
Correct |
246 ms |
196944 KB |
Output is correct |
20 |
Correct |
247 ms |
196944 KB |
Output is correct |
21 |
Correct |
260 ms |
196848 KB |
Output is correct |
22 |
Correct |
263 ms |
196944 KB |
Output is correct |
23 |
Correct |
263 ms |
196692 KB |
Output is correct |
24 |
Correct |
256 ms |
196948 KB |
Output is correct |
25 |
Correct |
1 ms |
2636 KB |
Output is correct |
26 |
Correct |
446 ms |
196708 KB |
Output is correct |
27 |
Correct |
401 ms |
196864 KB |
Output is correct |
28 |
Correct |
401 ms |
196728 KB |
Output is correct |
29 |
Correct |
420 ms |
196672 KB |
Output is correct |
30 |
Correct |
515 ms |
196420 KB |
Output is correct |
31 |
Correct |
500 ms |
196568 KB |
Output is correct |
32 |
Correct |
497 ms |
196640 KB |
Output is correct |
33 |
Correct |
498 ms |
196556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
4680 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14924 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14924 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
249 ms |
196784 KB |
Output is correct |
19 |
Correct |
246 ms |
196944 KB |
Output is correct |
20 |
Correct |
247 ms |
196944 KB |
Output is correct |
21 |
Correct |
260 ms |
196848 KB |
Output is correct |
22 |
Correct |
263 ms |
196944 KB |
Output is correct |
23 |
Correct |
263 ms |
196692 KB |
Output is correct |
24 |
Correct |
256 ms |
196948 KB |
Output is correct |
25 |
Runtime error |
6 ms |
2396 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Runtime error |
6 ms |
2396 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
4680 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14924 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14924 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
249 ms |
196784 KB |
Output is correct |
19 |
Correct |
246 ms |
196944 KB |
Output is correct |
20 |
Correct |
247 ms |
196944 KB |
Output is correct |
21 |
Correct |
260 ms |
196848 KB |
Output is correct |
22 |
Correct |
263 ms |
196944 KB |
Output is correct |
23 |
Correct |
263 ms |
196692 KB |
Output is correct |
24 |
Correct |
256 ms |
196948 KB |
Output is correct |
25 |
Correct |
1 ms |
2636 KB |
Output is correct |
26 |
Correct |
446 ms |
196708 KB |
Output is correct |
27 |
Correct |
401 ms |
196864 KB |
Output is correct |
28 |
Correct |
401 ms |
196728 KB |
Output is correct |
29 |
Correct |
420 ms |
196672 KB |
Output is correct |
30 |
Correct |
515 ms |
196420 KB |
Output is correct |
31 |
Correct |
500 ms |
196568 KB |
Output is correct |
32 |
Correct |
497 ms |
196640 KB |
Output is correct |
33 |
Correct |
498 ms |
196556 KB |
Output is correct |
34 |
Runtime error |
6 ms |
2396 KB |
Execution killed with signal 11 |
35 |
Halted |
0 ms |
0 KB |
- |