#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int msz = 2e5 + 5;
const int lg = 25;
using pii = pair<int, int>;
vector<int> a;
struct dsu{
vector<int> sz, p, m;
int n;
dsu(int ns){
n = ns;
p.resize(n + 1);
sz.resize(n + 1);
m.resize(n + 1);
for (int i = 1; i <= n; i++){
p[i] = m[i] = i;
sz[i] = 1;
}
}
int get(int v){
if (v != p[v]){
p[v] = get(p[v]);
}
return p[v];
}
void unite(int x, int y){
x = get(x); y = get(y);
if (x == y) return;
if (sz[x] > sz[y]){
swap(x, y);
}
p[x] = y;
if (a[m[x]] > a[m[y]]){
m[y] = m[x];
}
sz[y] += sz[x];
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
a.resize(n + 1);
vector<int> b;
int s = 0;
for (int i = 1; i <= n; i++){
cin>>a[i];
b.push_back(i);
if (a[i] == n){
s = i;
}
}
vector<int> g[n + 1];
for (int i = 1; i < n; i++){
int a, b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
auto cmp = [&](int x, int y){
return a[x] < a[y];
};
sort(b.begin(), b.end(), cmp);
vector<bool> used(n + 1);
dsu T(n + 1);
vector<int> p(n + 1);
b.pop_back();
for (int i: b){
used[i] = true;
vector<int> t;
for (int j: g[i]){
if (used[j]){
t.push_back(T.m[T.get(j)]);
}
}
for (int j: g[i]){
if (used[j]){
T.unite(i, j);
}
}
sort(t.begin(), t.end(), cmp);
for (int j = 0; j + 1 < t.size(); j++){
p[t[j + 1]] = t[j];
}
if (!t.empty()) p[i] = t.back();
}
fill(used.begin(), used.end(), false);
vector<int> d(n + 1), pw[n + 1], tin(n + 1), tout(n + 1);
for (int i = 1; i <= n; i++){
pw[i].resize(lg);
}
int timer = 0;
function<void(int, int)> fill = [&](int v, int pr){
tin[v] = timer++;
pw[v][0] = pr;
for (int i = 1; i < lg; i++){
pw[v][i] = pw[pw[v][i - 1]][i - 1];
}
for (int i: g[v]){
if (i == pr) continue;
d[i] = d[v] + 1;
fill(i, v);
}
tout[v] = timer++;
};
fill(1, 1);
auto check = [&](int x, int y){
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
};
auto lca = [&](int x, int y){
if (check(x, y)) return x;
if (check(y, x)) return y;
for (int i = lg - 1; i >= 0; i--){
if (!check(pw[x][i], y)){
x = pw[x][i];
}
}
return pw[x][0];
};
auto dist = [&](int x, int y){
return d[x] + d[y] - 2 * d[lca(x, y)];
};
for (int i = 1; i <= n; i++) used[i] = true;
for (int i = 1; i <= n; i++){
if (!p[i]){
used[i] = false;
continue;
}
used[p[i]] = false;
}
ll out = 0;
for (int i = 1; i <= n; i++){
if (used[i]){
int v = i;
ll sum = 0;
while (p[v] > 0){
sum += dist(v, p[v]);
v = p[v];
}
out = max(out, sum + dist(s, i));
}
}
cout<<out<<"\n";
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:87:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j = 0; j + 1 < t.size(); j++){
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
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 |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |