#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define ll long long
#define lld long double
#define st first
#define nd second
#define mp make_pair
#define ii pair<int, int>
#define vll vector<ll>
#define vi vector<int>
#define vvi vector<vector<int> >
#define pb push_back
#define eb emplace_back
#define remax(a, b) a = max(a, b);
#define remin(a, b) a = min(a, b);
#define all(x) x.begin(), x.end()
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define bit(n, x) (n & (1 << x))
#define rep(a, b) for(int a = 0; a < b; ++a)
using namespace std;
const int maxn = 2e5;
const int maxlog = 18;
vvi up(maxn, vi(maxlog));
vvi g(maxn);
vi par(maxn);
vi sz(maxn, 1);
vi depth(maxn);
vll ans(maxn, 0);
vi h(maxn);
vi mx(maxn); //vertex, h[mx[find_(v)]] to max h w spojnej z v
void dfs(int cur, int pare) {
depth[cur] = depth[pare]+1;
up[cur][0] = pare;
for (int i = 1; i < maxlog; ++i) up[cur][i] = up[up[cur][i-1]][i-1];
for (int u : g[cur]) {
if (u != pare) {
dfs(u, cur);
}
}
}
int dist(int u, int v) {
if (depth[u] < depth[v]) swap(u, v); //u jest glebiej niz v
int dt = depth[u]-depth[v];
for (int i = maxlog-1; i >= 0; --i) {
if (depth[up[u][i]] >= depth[v]) u = up[u][i]; //{cout << "hi " << u << endl; u = up[u][i];}
}
//teraz sa tak samo gleboko
if (u == v) return dt;
//teraz sa rozne, i na tej samej glebokosci
for (int i = maxlog-1; i >= 0; --i) {
if (up[u][i] != up[v][i]) {
u = up[u][i], v = up[v][i];
dt += (1<<(1+i));
}
}
return dt + 2;
}
int find_(int a) {
if (a == par[a]) return a;
return par[a] = find_(par[a]);
}
void join_(int a, int b) {
a = find_(a), b = find_(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
//remax(mx[a], h[mx[b]]);
if (h[mx[a]] < h[mx[b]]) mx[a] = mx[b];
remax(ans[a], ans[b]);
par[b] = a;
}
int main() {
fastio();
iota(all(par), 0), iota(all(mx), 0);
int n;
cin >> n;
vector<ii> q(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
q[i] = mp(h[i], i);
}
sort(all(q));
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a;--b;
g[a].eb(b);
g[b].eb(a);
}
depth[0] = -1;
dfs(0, 0);
/*rep(i, n) {
rep(j, n) {
cout << "i: " << i << " j: " << j << " d[i]: " << depth[i] << " d[j]: " << depth[j] << endl;
cout << "dist: " << dist(i, j) << endl;
}
}*/
for (int i = 0; i < n; ++i) {
const int cur = q[i].nd, curh = q[i].st;
for (int u : g[cur]) {
if (h[u] < curh) {
u = find_(u);
//remax(ans[u], ans[u]+dist(mx[u], cur));
ans[u] += dist(mx[u], cur);
join_(cur, u);
}
}
}
cout << ans[find_(0)] << '\n'; //+dist(n-1, q[n-1].nd) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
15 ms |
30812 KB |
Output is correct |
4 |
Correct |
15 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
15 ms |
30812 KB |
Output is correct |
9 |
Correct |
15 ms |
30812 KB |
Output is correct |
10 |
Correct |
15 ms |
30812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
15 ms |
30812 KB |
Output is correct |
4 |
Correct |
15 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
15 ms |
30812 KB |
Output is correct |
9 |
Correct |
15 ms |
30812 KB |
Output is correct |
10 |
Correct |
15 ms |
30812 KB |
Output is correct |
11 |
Correct |
16 ms |
30812 KB |
Output is correct |
12 |
Correct |
16 ms |
30808 KB |
Output is correct |
13 |
Correct |
15 ms |
31024 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
16 ms |
30808 KB |
Output is correct |
16 |
Correct |
16 ms |
30812 KB |
Output is correct |
17 |
Correct |
18 ms |
30808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
15 ms |
30812 KB |
Output is correct |
4 |
Correct |
15 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
15 ms |
30812 KB |
Output is correct |
9 |
Correct |
15 ms |
30812 KB |
Output is correct |
10 |
Correct |
15 ms |
30812 KB |
Output is correct |
11 |
Correct |
16 ms |
30812 KB |
Output is correct |
12 |
Correct |
16 ms |
30808 KB |
Output is correct |
13 |
Correct |
15 ms |
31024 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
16 ms |
30808 KB |
Output is correct |
16 |
Correct |
16 ms |
30812 KB |
Output is correct |
17 |
Correct |
18 ms |
30808 KB |
Output is correct |
18 |
Correct |
19 ms |
31324 KB |
Output is correct |
19 |
Correct |
18 ms |
31264 KB |
Output is correct |
20 |
Correct |
19 ms |
31324 KB |
Output is correct |
21 |
Correct |
20 ms |
31336 KB |
Output is correct |
22 |
Correct |
18 ms |
31320 KB |
Output is correct |
23 |
Correct |
19 ms |
31324 KB |
Output is correct |
24 |
Correct |
19 ms |
31320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
15 ms |
30812 KB |
Output is correct |
4 |
Correct |
15 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
15 ms |
30812 KB |
Output is correct |
9 |
Correct |
15 ms |
30812 KB |
Output is correct |
10 |
Correct |
15 ms |
30812 KB |
Output is correct |
11 |
Correct |
16 ms |
30812 KB |
Output is correct |
12 |
Correct |
16 ms |
30808 KB |
Output is correct |
13 |
Correct |
15 ms |
31024 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
16 ms |
30808 KB |
Output is correct |
16 |
Correct |
16 ms |
30812 KB |
Output is correct |
17 |
Correct |
18 ms |
30808 KB |
Output is correct |
18 |
Correct |
19 ms |
31324 KB |
Output is correct |
19 |
Correct |
18 ms |
31264 KB |
Output is correct |
20 |
Correct |
19 ms |
31324 KB |
Output is correct |
21 |
Correct |
20 ms |
31336 KB |
Output is correct |
22 |
Correct |
18 ms |
31320 KB |
Output is correct |
23 |
Correct |
19 ms |
31324 KB |
Output is correct |
24 |
Correct |
19 ms |
31320 KB |
Output is correct |
25 |
Correct |
19 ms |
30812 KB |
Output is correct |
26 |
Correct |
19 ms |
31520 KB |
Output is correct |
27 |
Correct |
18 ms |
31324 KB |
Output is correct |
28 |
Correct |
19 ms |
31068 KB |
Output is correct |
29 |
Correct |
18 ms |
31304 KB |
Output is correct |
30 |
Correct |
19 ms |
31064 KB |
Output is correct |
31 |
Correct |
20 ms |
31020 KB |
Output is correct |
32 |
Correct |
18 ms |
31068 KB |
Output is correct |
33 |
Correct |
18 ms |
31116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
15 ms |
30812 KB |
Output is correct |
4 |
Correct |
15 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
15 ms |
30812 KB |
Output is correct |
9 |
Correct |
15 ms |
30812 KB |
Output is correct |
10 |
Correct |
15 ms |
30812 KB |
Output is correct |
11 |
Correct |
16 ms |
30812 KB |
Output is correct |
12 |
Correct |
16 ms |
30808 KB |
Output is correct |
13 |
Correct |
15 ms |
31024 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
16 ms |
30808 KB |
Output is correct |
16 |
Correct |
16 ms |
30812 KB |
Output is correct |
17 |
Correct |
18 ms |
30808 KB |
Output is correct |
18 |
Correct |
19 ms |
31324 KB |
Output is correct |
19 |
Correct |
18 ms |
31264 KB |
Output is correct |
20 |
Correct |
19 ms |
31324 KB |
Output is correct |
21 |
Correct |
20 ms |
31336 KB |
Output is correct |
22 |
Correct |
18 ms |
31320 KB |
Output is correct |
23 |
Correct |
19 ms |
31324 KB |
Output is correct |
24 |
Correct |
19 ms |
31320 KB |
Output is correct |
25 |
Correct |
135 ms |
48208 KB |
Output is correct |
26 |
Correct |
139 ms |
47956 KB |
Output is correct |
27 |
Correct |
133 ms |
48212 KB |
Output is correct |
28 |
Correct |
189 ms |
47956 KB |
Output is correct |
29 |
Correct |
191 ms |
47980 KB |
Output is correct |
30 |
Correct |
193 ms |
48208 KB |
Output is correct |
31 |
Correct |
190 ms |
48208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30808 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
197 ms |
38972 KB |
Output is correct |
4 |
Correct |
208 ms |
38996 KB |
Output is correct |
5 |
Correct |
195 ms |
38980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30812 KB |
Output is correct |
2 |
Correct |
15 ms |
30812 KB |
Output is correct |
3 |
Correct |
15 ms |
30812 KB |
Output is correct |
4 |
Correct |
15 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30812 KB |
Output is correct |
6 |
Correct |
16 ms |
30812 KB |
Output is correct |
7 |
Correct |
16 ms |
30812 KB |
Output is correct |
8 |
Correct |
15 ms |
30812 KB |
Output is correct |
9 |
Correct |
15 ms |
30812 KB |
Output is correct |
10 |
Correct |
15 ms |
30812 KB |
Output is correct |
11 |
Correct |
16 ms |
30812 KB |
Output is correct |
12 |
Correct |
16 ms |
30808 KB |
Output is correct |
13 |
Correct |
15 ms |
31024 KB |
Output is correct |
14 |
Correct |
16 ms |
30812 KB |
Output is correct |
15 |
Correct |
16 ms |
30808 KB |
Output is correct |
16 |
Correct |
16 ms |
30812 KB |
Output is correct |
17 |
Correct |
18 ms |
30808 KB |
Output is correct |
18 |
Correct |
19 ms |
31324 KB |
Output is correct |
19 |
Correct |
18 ms |
31264 KB |
Output is correct |
20 |
Correct |
19 ms |
31324 KB |
Output is correct |
21 |
Correct |
20 ms |
31336 KB |
Output is correct |
22 |
Correct |
18 ms |
31320 KB |
Output is correct |
23 |
Correct |
19 ms |
31324 KB |
Output is correct |
24 |
Correct |
19 ms |
31320 KB |
Output is correct |
25 |
Correct |
19 ms |
30812 KB |
Output is correct |
26 |
Correct |
19 ms |
31520 KB |
Output is correct |
27 |
Correct |
18 ms |
31324 KB |
Output is correct |
28 |
Correct |
19 ms |
31068 KB |
Output is correct |
29 |
Correct |
18 ms |
31304 KB |
Output is correct |
30 |
Correct |
19 ms |
31064 KB |
Output is correct |
31 |
Correct |
20 ms |
31020 KB |
Output is correct |
32 |
Correct |
18 ms |
31068 KB |
Output is correct |
33 |
Correct |
18 ms |
31116 KB |
Output is correct |
34 |
Correct |
135 ms |
48208 KB |
Output is correct |
35 |
Correct |
139 ms |
47956 KB |
Output is correct |
36 |
Correct |
133 ms |
48212 KB |
Output is correct |
37 |
Correct |
189 ms |
47956 KB |
Output is correct |
38 |
Correct |
191 ms |
47980 KB |
Output is correct |
39 |
Correct |
193 ms |
48208 KB |
Output is correct |
40 |
Correct |
190 ms |
48208 KB |
Output is correct |
41 |
Correct |
15 ms |
30808 KB |
Output is correct |
42 |
Correct |
15 ms |
30812 KB |
Output is correct |
43 |
Correct |
197 ms |
38972 KB |
Output is correct |
44 |
Correct |
208 ms |
38996 KB |
Output is correct |
45 |
Correct |
195 ms |
38980 KB |
Output is correct |
46 |
Correct |
297 ms |
44012 KB |
Output is correct |
47 |
Correct |
308 ms |
43088 KB |
Output is correct |
48 |
Correct |
278 ms |
44368 KB |
Output is correct |
49 |
Correct |
303 ms |
43092 KB |
Output is correct |
50 |
Correct |
242 ms |
38736 KB |
Output is correct |
51 |
Correct |
256 ms |
38956 KB |
Output is correct |
52 |
Correct |
259 ms |
38928 KB |
Output is correct |
53 |
Correct |
252 ms |
38720 KB |
Output is correct |