#include <bits/stdc++.h>
struct DSU {
std::vector<int> height;
std::vector<int> boss;
explicit DSU(int N) {
boss.resize(1 + N);
height.resize(1 + N, 1);
for (int i = 1; i <= N; i++)
boss[i] = i;
}
int find(int x) {
if (x == boss[x])
return x;
boss[x] = find(boss[x]);
return boss[x];
}
void merge(int a, int b) {
boss[find(a)] = find(b);
}
};
int main() {
std::cin.tie(NULL);
std::iostream::sync_with_stdio(false);
int N;
std::cin >> N;
std::vector<int> perm(1 + N);
for (int i = 1; i <= N; i++)
std::cin >> perm[i];
std::vector<std::vector<int>> graph(1 + N);
for (int i = 0; i < N - 1; i++) {
int a, b;
std::cin >> a >> b;
a = perm[a];
b = perm[b];
graph[a].push_back(b);
graph[b].push_back(a);
}
DSU dsu(N);
for (int node = 1; node <= N; node++) {
int height = 0;
for (auto it: graph[node]) {
if (node > it) {
height = std::max(height, dsu.height[dsu.find(it)]);
dsu.merge(node, it);
}
}
dsu.height[dsu.find(node)] = 1 + height;
}
std::cout << dsu.height[dsu.find(N)];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |