#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int N = 2e5 + 7, L = 19;
int n;
int p[N];
vector<int> adj[N];
int lift[N][L], dep[N], dp[N], e[N];
int get(int x) {
return e[x] == x ? x : e[x] = get(e[x]);
}
void dfs(int v = 1, int p = 0, int d = 0) {
dep[v] = d;
lift[v][0] = p;
for (int i = 1; i < L; i++)
lift[v][i] = lift[lift[v][i - 1]][i - 1];
for (int u : adj[v])
if (u != p)
dfs(u, v, d + 1);
}
int lca(int u, int v) {
if (dep[u] > dep[v])
swap(u, v);
for (int i = L - 1; ~i; --i)
if (dep[v] - (1 << i) >= dep[u])
v = lift[v][i];
if (u == v)
return u;
for (int i = L - 1; ~i; --i)
if (lift[v][i] != lift[u][i])
v = lift[v][i], u = lift[u][i];
return lift[u][0];
}
int dist(int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
u = p[u], v = p[v];
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs();
iota(e + 1, e + n + 1, 1);
for (int i = 1; i <= n; i++) {
for (int u : adj[i]) {
u = get(u);
if (u < i) {
dp[i] = max(dp[i], dp[u] + dist(i, u));
e[u] = i;
}
}
}
cout << dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9884 KB |
Output is correct |
3 |
Correct |
2 ms |
9888 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9880 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9884 KB |
Output is correct |
3 |
Correct |
2 ms |
9888 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9880 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9912 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9816 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9884 KB |
Output is correct |
3 |
Correct |
2 ms |
9888 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9880 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9912 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9816 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
10072 KB |
Output is correct |
19 |
Correct |
4 ms |
10332 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
4 ms |
10228 KB |
Output is correct |
22 |
Correct |
4 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10076 KB |
Output is correct |
24 |
Correct |
4 ms |
10328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9884 KB |
Output is correct |
3 |
Correct |
2 ms |
9888 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9880 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9912 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9816 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
10072 KB |
Output is correct |
19 |
Correct |
4 ms |
10332 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
4 ms |
10228 KB |
Output is correct |
22 |
Correct |
4 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10076 KB |
Output is correct |
24 |
Correct |
4 ms |
10328 KB |
Output is correct |
25 |
Correct |
2 ms |
9816 KB |
Output is correct |
26 |
Correct |
4 ms |
10076 KB |
Output is correct |
27 |
Correct |
4 ms |
10076 KB |
Output is correct |
28 |
Correct |
4 ms |
10076 KB |
Output is correct |
29 |
Correct |
4 ms |
10076 KB |
Output is correct |
30 |
Correct |
4 ms |
10076 KB |
Output is correct |
31 |
Correct |
4 ms |
10160 KB |
Output is correct |
32 |
Correct |
5 ms |
10076 KB |
Output is correct |
33 |
Correct |
4 ms |
10072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9884 KB |
Output is correct |
3 |
Correct |
2 ms |
9888 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9880 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9912 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9816 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
10072 KB |
Output is correct |
19 |
Correct |
4 ms |
10332 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
4 ms |
10228 KB |
Output is correct |
22 |
Correct |
4 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10076 KB |
Output is correct |
24 |
Correct |
4 ms |
10328 KB |
Output is correct |
25 |
Incorrect |
98 ms |
37716 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
131 ms |
32892 KB |
Output is correct |
4 |
Correct |
140 ms |
32892 KB |
Output is correct |
5 |
Correct |
128 ms |
33068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9884 KB |
Output is correct |
3 |
Correct |
2 ms |
9888 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9880 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9912 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
2 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9820 KB |
Output is correct |
16 |
Correct |
2 ms |
9816 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
10072 KB |
Output is correct |
19 |
Correct |
4 ms |
10332 KB |
Output is correct |
20 |
Correct |
4 ms |
10332 KB |
Output is correct |
21 |
Correct |
4 ms |
10228 KB |
Output is correct |
22 |
Correct |
4 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10076 KB |
Output is correct |
24 |
Correct |
4 ms |
10328 KB |
Output is correct |
25 |
Correct |
2 ms |
9816 KB |
Output is correct |
26 |
Correct |
4 ms |
10076 KB |
Output is correct |
27 |
Correct |
4 ms |
10076 KB |
Output is correct |
28 |
Correct |
4 ms |
10076 KB |
Output is correct |
29 |
Correct |
4 ms |
10076 KB |
Output is correct |
30 |
Correct |
4 ms |
10076 KB |
Output is correct |
31 |
Correct |
4 ms |
10160 KB |
Output is correct |
32 |
Correct |
5 ms |
10076 KB |
Output is correct |
33 |
Correct |
4 ms |
10072 KB |
Output is correct |
34 |
Incorrect |
98 ms |
37716 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |