이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |