This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 2e5 + 5;
int a[N];
int dep[N];
vector<int> g[N];
vector<pair<int, int>> t[N];
int dfs(int root, int v, int p) {
if (a[v] > a[root]) {
return 0;
}
int ret = v;
for (int u : g[v]) {
if (u == p) {
continue;
}
dep[u] = dep[v] + 1;
int ret2 = dfs(root, u, v);
if (a[ret2] > a[ret]) {
ret = ret2;
}
if (v == root) {
t[v].push_back({ret2, dep[ret2]});
}
}
return ret;
}
long long dfs2(int v) {
long long ret = 0;
for (auto [u, w] : t[v]) {
long long ret2 = dfs2(u);
ret = max(ret, ret2 + w);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int root = 1;
for (int i = 1; i <= n; ++i) {
if (a[i] == n) {
root = i;
}
}
for (int i = 1; i <= n; ++i) {
dep[i] = 0;
dfs(i, i, i);
}
cout << dfs2(root);
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... |