#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LOG = 20;
template<typename T>
bool assign_min(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_max(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct DSU {
vector<ll> p;
DSU(ll n) {
p.resize(n, 0);
for (ll i = 0; i < n; i++) {
p[i] = i;
}
}
ll get(ll x) {
return (p[x] == x ? x : p[x] = get(p[x]));
}
void unite(ll a, ll b) {
p[b] = a;
}
};
struct LCA {
vector<vector<ll>> tree;
vector<ll> h;
vector<vector<ll>> binup;
LCA(vector<vector<ll>> tree): tree(tree) {
ll n = tree.size();
h.resize(n, -1);
binup.resize(n, vector<ll>(LOG, 0));
dfs(0, 0);
}
void dfs(ll v, ll p) {
h[v] = h[p] + 1;
binup[v][0] = p;
for (ll i = 1; i < LOG; i++) {
binup[v][i] = binup[binup[v][i - 1]][i - 1];
}
for (auto i : tree[v]) {
if (i != p) {
dfs(i, v);
}
}
}
ll la(ll v, ll x) {
for (ll i = 0; i < LOG; i++) {
if ((x >> i) & 1) {
v = binup[v][i];
}
}
return v;
}
ll lca(ll v, ll u) {
if (h[v] < h[u]) {
swap(v, u);
}
v = la(v, h[v] - h[u]);
if (v == u) {
return v;
}
for (ll i = LOG - 1; i >= 0; i--) {
if (binup[v][i] != binup[u][i]) {
v = binup[v][i];
u = binup[u][i];
}
}
return binup[v][1];
}
ll dist(ll v, ll u) {
return h[v] + h[u] - h[lca(v, u)] * 2;
}
};
void solve() {
ll n;
cin >> n;
vector<pair<ll, ll>> arr(n);
for (ll i = 0; i < n; i++) {
cin >> arr[i].first;
arr[i].second = i;
}
sort(arr.begin(), arr.end());
vector<vector<ll>> tree(n);
for (ll i = 1; i < n; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
DSU d(n);
LCA l(tree);
vector<ll> dp(n, -1);
ll ans = 0;
for (auto[_, i] : arr) {
dp[i] = 0;
for (auto j : tree[i]) {
if (dp[j] >= 0) {
ll x = d.get(j);
assign_max(dp[i], dp[x] + l.dist(i, x));
d.unite(i, x);
}
}
assign_max(ans, dp[i]);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2508 KB |
Output is correct |
19 |
Correct |
4 ms |
2504 KB |
Output is correct |
20 |
Correct |
4 ms |
2388 KB |
Output is correct |
21 |
Correct |
5 ms |
2508 KB |
Output is correct |
22 |
Correct |
4 ms |
2388 KB |
Output is correct |
23 |
Correct |
4 ms |
2388 KB |
Output is correct |
24 |
Correct |
4 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2508 KB |
Output is correct |
19 |
Correct |
4 ms |
2504 KB |
Output is correct |
20 |
Correct |
4 ms |
2388 KB |
Output is correct |
21 |
Correct |
5 ms |
2508 KB |
Output is correct |
22 |
Correct |
4 ms |
2388 KB |
Output is correct |
23 |
Correct |
4 ms |
2388 KB |
Output is correct |
24 |
Correct |
4 ms |
2388 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
5 ms |
2376 KB |
Output is correct |
27 |
Incorrect |
7 ms |
2388 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2508 KB |
Output is correct |
19 |
Correct |
4 ms |
2504 KB |
Output is correct |
20 |
Correct |
4 ms |
2388 KB |
Output is correct |
21 |
Correct |
5 ms |
2508 KB |
Output is correct |
22 |
Correct |
4 ms |
2388 KB |
Output is correct |
23 |
Correct |
4 ms |
2388 KB |
Output is correct |
24 |
Correct |
4 ms |
2388 KB |
Output is correct |
25 |
Correct |
170 ms |
89176 KB |
Output is correct |
26 |
Correct |
172 ms |
89316 KB |
Output is correct |
27 |
Correct |
165 ms |
89196 KB |
Output is correct |
28 |
Correct |
213 ms |
89160 KB |
Output is correct |
29 |
Correct |
223 ms |
88968 KB |
Output is correct |
30 |
Correct |
218 ms |
89084 KB |
Output is correct |
31 |
Correct |
214 ms |
89324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2508 KB |
Output is correct |
19 |
Correct |
4 ms |
2504 KB |
Output is correct |
20 |
Correct |
4 ms |
2388 KB |
Output is correct |
21 |
Correct |
5 ms |
2508 KB |
Output is correct |
22 |
Correct |
4 ms |
2388 KB |
Output is correct |
23 |
Correct |
4 ms |
2388 KB |
Output is correct |
24 |
Correct |
4 ms |
2388 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
5 ms |
2376 KB |
Output is correct |
27 |
Incorrect |
7 ms |
2388 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |