#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 6000 + 5;
const int MAX_K = 13;
vector<int> g[MAX_N];
bool used[MAX_N];
int tin[MAX_N];
int tout[MAX_N];
int timer = 0;
int binup[MAX_K][MAX_N];
int max_vertex[MAX_K][MAX_N];
int depth[MAX_N];
void dfs(int v) {
tin[v] = timer++;
for (auto u : g[v]) {
g[u].erase(find(g[u].begin(), g[u].end(), v));
binup[0][u] = v;
max_vertex[0][v] = v;
depth[u] = depth[v] + 1;
dfs(u);
}
tout[v] = timer++;
}
bool is_parent(int p, int v) {
return (tin[p] <= tin[v] && tout[v] <= tout[p]);
}
int lca(int u, int v) {
if (is_parent(u, v)) return u;
if (is_parent(v, u)) return v;
for (int k = MAX_K - 1; k >= 0; --k) {
if (!is_parent(binup[k][v], u)) {
v = binup[k][v];
}
}
return binup[0][v];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int max_v(int u, int v) {
if (is_parent(u, v)) {
v = binup[0][v];
int w = depth[v] - depth[u];
int res = -1;
for (int k = MAX_K - 1; k >= 0; --k) {
if (w & (1 << k)) {
res = max(res, max_vertex[k][v]);
v = binup[k][v];
}
}
return res;
} else if (is_parent(v, u)) {
u = binup[0][u];
int w = depth[u] - depth[v];
int res = -1;
for (int k = MAX_K - 1; k >= 0; --k) {
if (w & (1 << k)) {
res = max(res, max_vertex[k][u]);
u = binup[k][u];
}
}
return res;
} else {
v = binup[0][v];
u = binup[0][u];
int c = lca(u, v);
int w1 = depth[u] - depth[c];
int res1 = -1;
for (int k = MAX_K - 1; k >= 0; --k) {
if (w1 & (1 << k)) {
res1 = max(res1, max_vertex[k][u]);
u = binup[k][u];
}
}
int w2 = depth[v] - depth[c];
int res2 = -1;
for (int k = MAX_K - 1; k >= 0; --k) {
if (w2 & (1 << k)) {
res2 = max(res2, max_vertex[k][v]);
v = binup[k][v];
}
}
return max(c, max(res1, res2));
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> p(n);
for (auto& x : p) {
cin >> x;
--x;
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
u = p[u];
v = p[v];
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
for (int k = 1; k < MAX_K; ++k) {
for (int v = 0; v < n; ++v) {
binup[k][v] = binup[k - 1][binup[k - 1][v]];
max_vertex[k][v] = max(max_vertex[k - 1][v], max_vertex[k - 1][binup[k - 1][v]]);
}
}
vector<int64_t> dp(n, -1e9);
dp.back() = 0;
int64_t res = 0;
for (int v = n - 2; v >= 0; --v) {
for (int u = v + 1; u < n; ++u) {
// dbg(u);
// dbg(v);
// dbg(max_v(u, v));
if (max_v(u, v) < min(u, v)) {
dp[v] = max(dp[v], dp[u] + dist(u, v));
}
}
res = max(res, dp[v]);
// cerr << endl;
// dbg(v);
// dbg(dp[v]);
// cerr << endl;
}
// dbg(max_v(2, 0));
cout << res << '\n';
return 0;
}
/*
16
16 8 7 9 1 5 3 13 11 15 12 10 2 4 14 6
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
22
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
784 KB |
Output is correct |
14 |
Correct |
5 ms |
776 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
784 KB |
Output is correct |
14 |
Correct |
5 ms |
776 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
965 ms |
1664 KB |
Output is correct |
19 |
Correct |
559 ms |
1860 KB |
Output is correct |
20 |
Correct |
568 ms |
1884 KB |
Output is correct |
21 |
Correct |
1373 ms |
1876 KB |
Output is correct |
22 |
Correct |
940 ms |
1832 KB |
Output is correct |
23 |
Correct |
1320 ms |
1720 KB |
Output is correct |
24 |
Correct |
1016 ms |
1828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
784 KB |
Output is correct |
14 |
Correct |
5 ms |
776 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
965 ms |
1664 KB |
Output is correct |
19 |
Correct |
559 ms |
1860 KB |
Output is correct |
20 |
Correct |
568 ms |
1884 KB |
Output is correct |
21 |
Correct |
1373 ms |
1876 KB |
Output is correct |
22 |
Correct |
940 ms |
1832 KB |
Output is correct |
23 |
Correct |
1320 ms |
1720 KB |
Output is correct |
24 |
Correct |
1016 ms |
1828 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
839 ms |
1744 KB |
Output is correct |
27 |
Correct |
863 ms |
1628 KB |
Output is correct |
28 |
Correct |
852 ms |
1744 KB |
Output is correct |
29 |
Correct |
819 ms |
1748 KB |
Output is correct |
30 |
Correct |
1453 ms |
1480 KB |
Output is correct |
31 |
Correct |
1541 ms |
1480 KB |
Output is correct |
32 |
Correct |
1551 ms |
1480 KB |
Output is correct |
33 |
Correct |
1529 ms |
1476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
784 KB |
Output is correct |
14 |
Correct |
5 ms |
776 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
965 ms |
1664 KB |
Output is correct |
19 |
Correct |
559 ms |
1860 KB |
Output is correct |
20 |
Correct |
568 ms |
1884 KB |
Output is correct |
21 |
Correct |
1373 ms |
1876 KB |
Output is correct |
22 |
Correct |
940 ms |
1832 KB |
Output is correct |
23 |
Correct |
1320 ms |
1720 KB |
Output is correct |
24 |
Correct |
1016 ms |
1828 KB |
Output is correct |
25 |
Runtime error |
13 ms |
2392 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Runtime error |
15 ms |
2496 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
784 KB |
Output is correct |
14 |
Correct |
5 ms |
776 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
965 ms |
1664 KB |
Output is correct |
19 |
Correct |
559 ms |
1860 KB |
Output is correct |
20 |
Correct |
568 ms |
1884 KB |
Output is correct |
21 |
Correct |
1373 ms |
1876 KB |
Output is correct |
22 |
Correct |
940 ms |
1832 KB |
Output is correct |
23 |
Correct |
1320 ms |
1720 KB |
Output is correct |
24 |
Correct |
1016 ms |
1828 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
839 ms |
1744 KB |
Output is correct |
27 |
Correct |
863 ms |
1628 KB |
Output is correct |
28 |
Correct |
852 ms |
1744 KB |
Output is correct |
29 |
Correct |
819 ms |
1748 KB |
Output is correct |
30 |
Correct |
1453 ms |
1480 KB |
Output is correct |
31 |
Correct |
1541 ms |
1480 KB |
Output is correct |
32 |
Correct |
1551 ms |
1480 KB |
Output is correct |
33 |
Correct |
1529 ms |
1476 KB |
Output is correct |
34 |
Runtime error |
13 ms |
2392 KB |
Execution killed with signal 11 |
35 |
Halted |
0 ms |
0 KB |
- |