#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);
}
}
ans = dp[i];
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2388 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
4 ms |
2388 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 |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2388 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
4 ms |
2388 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 |
4 ms |
2316 KB |
Output is correct |
27 |
Incorrect |
5 ms |
2388 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2388 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
4 ms |
2388 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 |
169 ms |
85556 KB |
Output is correct |
26 |
Correct |
180 ms |
85428 KB |
Output is correct |
27 |
Correct |
163 ms |
85500 KB |
Output is correct |
28 |
Correct |
239 ms |
85484 KB |
Output is correct |
29 |
Correct |
205 ms |
85536 KB |
Output is correct |
30 |
Correct |
207 ms |
85448 KB |
Output is correct |
31 |
Correct |
208 ms |
85544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2388 KB |
Output is correct |
19 |
Correct |
6 ms |
2424 KB |
Output is correct |
20 |
Correct |
3 ms |
2388 KB |
Output is correct |
21 |
Correct |
4 ms |
2388 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 |
4 ms |
2316 KB |
Output is correct |
27 |
Incorrect |
5 ms |
2388 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |