#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define INF 1e18
using namespace std;
typedef long long ll;
vector<int>adj[200005];
int d[200005],par[200005], p[200005][20], dp[200005];
void dfs(int s, int e) {
for (int i = 1; i <=18; i++) {
p[s][i] = p[p[s][i - 1]][i - 1];
}
for (auto u : adj[s]) {
if (u == e) continue;
d[u] = d[s] + 1;
p[u][0] = s;
dfs(u, s);
}
}
int lca(int a, int b) {
if (d[a] > d[b]) swap(a, b);
int dist = d[b] - d[a];
for (int i = 0; i <= 18; i++) {
if (dist & (1 << i))b = p[b][i];
}
if (a == b) return a;
for (int i = 18; i >= 0; i--) {
if (p[a][i] != p[b][i]) {
a = p[a][i];
b = p[b][i];
}
}
return p[a][0];
}
int find(int x) {
while (x != par[x])x = par[x];
return x;
}
int get_dist(int a, int b) {
int lcaa = lca(a, b);
return (d[a] - d[lcaa]) + (d[b] - d[lcaa]);
}
void solve()
{
int n; cin >> n;
vector<int>v(n + 1);
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n - 1; i++) {
int a, b; cin >> a >> b;
a = v[a]; b = v[b];
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0);
for (int i = 1; i <= n; i++) par[i] = i;
for (int i = 1; i <= n; i++) {
for (auto u : adj[i]) {
u = find(u);
if (u < i) {
par[u] = i;
dp[i] = max(dp[i], dp[u] + get_dist(i, u));
}
}
}
cout << dp[n] << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;// cin>>t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8612 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8624 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8612 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8624 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8608 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8620 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8612 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8624 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8608 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8620 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8536 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9656 KB |
Output is correct |
20 |
Correct |
4 ms |
9644 KB |
Output is correct |
21 |
Correct |
4 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9564 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
4 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8612 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8624 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8608 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8620 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8536 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9656 KB |
Output is correct |
20 |
Correct |
4 ms |
9644 KB |
Output is correct |
21 |
Correct |
4 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9564 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
4 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
9468 KB |
Output is correct |
27 |
Correct |
4 ms |
9308 KB |
Output is correct |
28 |
Correct |
4 ms |
9308 KB |
Output is correct |
29 |
Correct |
4 ms |
9308 KB |
Output is correct |
30 |
Correct |
5 ms |
9052 KB |
Output is correct |
31 |
Correct |
5 ms |
9132 KB |
Output is correct |
32 |
Correct |
4 ms |
9212 KB |
Output is correct |
33 |
Correct |
5 ms |
9216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8612 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8624 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8608 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8620 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8536 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9656 KB |
Output is correct |
20 |
Correct |
4 ms |
9644 KB |
Output is correct |
21 |
Correct |
4 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9564 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
4 ms |
9564 KB |
Output is correct |
25 |
Incorrect |
95 ms |
41728 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
295 ms |
33848 KB |
Output is correct |
4 |
Correct |
310 ms |
33872 KB |
Output is correct |
5 |
Correct |
249 ms |
33704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
3 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8612 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8624 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8536 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8608 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8620 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8536 KB |
Output is correct |
18 |
Correct |
4 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9656 KB |
Output is correct |
20 |
Correct |
4 ms |
9644 KB |
Output is correct |
21 |
Correct |
4 ms |
9308 KB |
Output is correct |
22 |
Correct |
5 ms |
9564 KB |
Output is correct |
23 |
Correct |
4 ms |
9304 KB |
Output is correct |
24 |
Correct |
4 ms |
9564 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
9468 KB |
Output is correct |
27 |
Correct |
4 ms |
9308 KB |
Output is correct |
28 |
Correct |
4 ms |
9308 KB |
Output is correct |
29 |
Correct |
4 ms |
9308 KB |
Output is correct |
30 |
Correct |
5 ms |
9052 KB |
Output is correct |
31 |
Correct |
5 ms |
9132 KB |
Output is correct |
32 |
Correct |
4 ms |
9212 KB |
Output is correct |
33 |
Correct |
5 ms |
9216 KB |
Output is correct |
34 |
Incorrect |
95 ms |
41728 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |