#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5, MAX_LOG = 18;
vector<int> G[MAX_N];
int ancestor[MAX_LOG][MAX_N] = {}, depth[MAX_N] = {};
void DFS(int current, int parent) {
ancestor[0][current] = parent;
for (int step = 0; step < MAX_LOG - 1; step++) {
ancestor[step + 1][current] = ancestor[current][ancestor[step][current]];
}
for (int child : G[current]) {
if (child != parent) {
depth[child] = depth[current] + 1;
DFS(child, current);
}
}
}
int distance(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
int answer = depth[u] - depth[v];
for (int step = MAX_LOG - 1; step >= 0; step--) {
if (depth[u] - (1 << step) >= depth[v]) {
u = ancestor[step][u];
}
}
if (u == v) {
return answer;
}
for (int step = MAX_LOG - 1; step >= 0; step--) {
if (ancestor[step][u] != ancestor[step][v]) {
u = ancestor[step][u];
v = ancestor[step][v];
answer += 2 * (1 << step);
}
}
answer += 2;
return answer;
}
struct dsu {
vector<int> parent;
dsu(int size) : parent(size, -1) {}
int leader(int u) {
if (parent[u] == -1) {
return u;
} else {
parent[u] = leader(parent[u]);
return parent[u];
}
}
void unite(int u, int v) {
u = leader(u);
v = leader(v);
if (u != v) {
if (u < v) {
swap(u, v);
}
parent[v] = u;
}
}
};
int main() {
int N;
cin >> N;
vector<int> P(N);
for (int i = 0; i < N; i++) {
cin >> P[i];
P[i]--;
}
vector<int> position(N);
for (int i = 0; i < N; i++) {
position[P[i]] = i;
}
for (int i = 0; i < N - 1; i++) {
int A, B;
cin >> A >> B;
A--, B--;
G[P[A]].push_back(P[B]);
G[P[B]].push_back(P[A]);
}
DFS(0, 0);
dsu sets(N);
vector<long long> component_result(N);
for (int i = 0; i < N; i++) {
for (int j : G[i]) {
if (j < i) {
j = sets.leader(j);
component_result[i] = max(component_result[i], component_result[j] + distance(i, j));
sets.unite(i, j);
}
}
if (i + 1 == N) {
cout << component_result[N - 1] << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Runtime error |
7 ms |
10068 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |