#include <bits/stdc++.h>
using namespace std;
vector<long long> h(200001);
vector<long long> haha[200001];
vector<long long> dsu(200001);
vector<long long> dep(200001);
long long bruh[200001][19];
void dfs(long long a, long long t, long long d) {
bruh[a][0] = t;
dep[a] = d;
for(long long v: haha[a]) {
if(v != t) {
dfs(v,a,d+1);
}
}
}
long long check(long long a) {
long long c = a,d,e;
while(dsu[c] != c) {
c = dsu[c];
}
d = a;
while(d != dsu[d]) {
e = dsu[d];
dsu[d] = c;
d = e;
}
return c;
}
long long dist(long long a, long long b) {
if(dep[a] < dep[b]) {
swap(a,b);
}
long long c = dep[a]-dep[b],e = a,d = b;
for(long long i = 0; i < 19; i++) {
if((1 << i)&c) {
e = bruh[e][i];
}
}
if(b != e) {
for(long long i = 18; i >= 0; i--) {
if(bruh[e][i] != -1 && bruh[e][i] != bruh[b][i]) {
e = bruh[e][i];
b = bruh[b][i];
}
}
e = bruh[e][0];
}
return dep[a]-dep[e]+dep[d]-dep[e];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for(long long i = 1; i <= 200000; i++) {
dsu[i] = i;
}
long long n,a,b,y = 0;
cin >> n;
for(long long i = 1; i <= n; i++) {
cin >> h[i];
}
vector<pair<long long,pair<long long,long long>>> edge(0);
for(long long i = 0; i < n-1; i++) {
cin >> a >> b;
haha[a].push_back(b);
haha[b].push_back(a);
edge.push_back({max(h[a],h[b]),{a,b}});
}
sort(edge.begin(),edge.end());
dfs(1,-1,0);
for(long long i = 1; i < 19; i++) {
for(long long j = 1; j <= n; j++) {
if(bruh[j][i-1] != -1) {
bruh[j][i] = bruh[bruh[j][i-1]][i-1];
}
else {
bruh[j][i] = -1;
}
}
}
vector<long long> ans(n+1);
for(long long i = 1; i <= n; i++) {
while(y < n-1 && edge[y].first == i) {
a = edge[y].second.first;
b = edge[y].second.second;
if(h[a] < h[b]) {
swap(a,b);
}
b = check(b);
dsu[b] = a;
ans[a] = max(ans[a],dist(a,b)+ans[b]);
y++;
}
}
for(long long i = 1; i <= n; i++) {
if(h[i] == n) {
cout << ans[i];
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9736 KB |
Output is correct |
16 |
Correct |
4 ms |
9812 KB |
Output is correct |
17 |
Correct |
4 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9736 KB |
Output is correct |
16 |
Correct |
4 ms |
9812 KB |
Output is correct |
17 |
Correct |
4 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
11092 KB |
Output is correct |
19 |
Correct |
7 ms |
11092 KB |
Output is correct |
20 |
Correct |
8 ms |
11084 KB |
Output is correct |
21 |
Correct |
7 ms |
11092 KB |
Output is correct |
22 |
Correct |
7 ms |
10996 KB |
Output is correct |
23 |
Correct |
7 ms |
11040 KB |
Output is correct |
24 |
Correct |
7 ms |
11024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9736 KB |
Output is correct |
16 |
Correct |
4 ms |
9812 KB |
Output is correct |
17 |
Correct |
4 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
11092 KB |
Output is correct |
19 |
Correct |
7 ms |
11092 KB |
Output is correct |
20 |
Correct |
8 ms |
11084 KB |
Output is correct |
21 |
Correct |
7 ms |
11092 KB |
Output is correct |
22 |
Correct |
7 ms |
10996 KB |
Output is correct |
23 |
Correct |
7 ms |
11040 KB |
Output is correct |
24 |
Correct |
7 ms |
11024 KB |
Output is correct |
25 |
Correct |
4 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
10964 KB |
Output is correct |
27 |
Correct |
9 ms |
10936 KB |
Output is correct |
28 |
Correct |
9 ms |
10964 KB |
Output is correct |
29 |
Correct |
7 ms |
11028 KB |
Output is correct |
30 |
Correct |
8 ms |
10836 KB |
Output is correct |
31 |
Correct |
8 ms |
10896 KB |
Output is correct |
32 |
Correct |
7 ms |
10900 KB |
Output is correct |
33 |
Correct |
7 ms |
10836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9736 KB |
Output is correct |
16 |
Correct |
4 ms |
9812 KB |
Output is correct |
17 |
Correct |
4 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
11092 KB |
Output is correct |
19 |
Correct |
7 ms |
11092 KB |
Output is correct |
20 |
Correct |
8 ms |
11084 KB |
Output is correct |
21 |
Correct |
7 ms |
11092 KB |
Output is correct |
22 |
Correct |
7 ms |
10996 KB |
Output is correct |
23 |
Correct |
7 ms |
11040 KB |
Output is correct |
24 |
Correct |
7 ms |
11024 KB |
Output is correct |
25 |
Correct |
157 ms |
63784 KB |
Output is correct |
26 |
Correct |
173 ms |
65040 KB |
Output is correct |
27 |
Correct |
144 ms |
65100 KB |
Output is correct |
28 |
Correct |
171 ms |
65132 KB |
Output is correct |
29 |
Correct |
151 ms |
65084 KB |
Output is correct |
30 |
Correct |
159 ms |
65136 KB |
Output is correct |
31 |
Correct |
156 ms |
65040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9644 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
168 ms |
57156 KB |
Output is correct |
4 |
Correct |
189 ms |
57200 KB |
Output is correct |
5 |
Correct |
167 ms |
57124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
4 ms |
9812 KB |
Output is correct |
15 |
Correct |
4 ms |
9736 KB |
Output is correct |
16 |
Correct |
4 ms |
9812 KB |
Output is correct |
17 |
Correct |
4 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
11092 KB |
Output is correct |
19 |
Correct |
7 ms |
11092 KB |
Output is correct |
20 |
Correct |
8 ms |
11084 KB |
Output is correct |
21 |
Correct |
7 ms |
11092 KB |
Output is correct |
22 |
Correct |
7 ms |
10996 KB |
Output is correct |
23 |
Correct |
7 ms |
11040 KB |
Output is correct |
24 |
Correct |
7 ms |
11024 KB |
Output is correct |
25 |
Correct |
4 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
10964 KB |
Output is correct |
27 |
Correct |
9 ms |
10936 KB |
Output is correct |
28 |
Correct |
9 ms |
10964 KB |
Output is correct |
29 |
Correct |
7 ms |
11028 KB |
Output is correct |
30 |
Correct |
8 ms |
10836 KB |
Output is correct |
31 |
Correct |
8 ms |
10896 KB |
Output is correct |
32 |
Correct |
7 ms |
10900 KB |
Output is correct |
33 |
Correct |
7 ms |
10836 KB |
Output is correct |
34 |
Correct |
157 ms |
63784 KB |
Output is correct |
35 |
Correct |
173 ms |
65040 KB |
Output is correct |
36 |
Correct |
144 ms |
65100 KB |
Output is correct |
37 |
Correct |
171 ms |
65132 KB |
Output is correct |
38 |
Correct |
151 ms |
65084 KB |
Output is correct |
39 |
Correct |
159 ms |
65136 KB |
Output is correct |
40 |
Correct |
156 ms |
65040 KB |
Output is correct |
41 |
Correct |
4 ms |
9644 KB |
Output is correct |
42 |
Correct |
5 ms |
9684 KB |
Output is correct |
43 |
Correct |
168 ms |
57156 KB |
Output is correct |
44 |
Correct |
189 ms |
57200 KB |
Output is correct |
45 |
Correct |
167 ms |
57124 KB |
Output is correct |
46 |
Correct |
242 ms |
61368 KB |
Output is correct |
47 |
Correct |
228 ms |
60580 KB |
Output is correct |
48 |
Correct |
220 ms |
61824 KB |
Output is correct |
49 |
Correct |
250 ms |
60552 KB |
Output is correct |
50 |
Correct |
242 ms |
56788 KB |
Output is correct |
51 |
Correct |
247 ms |
56784 KB |
Output is correct |
52 |
Correct |
236 ms |
56780 KB |
Output is correct |
53 |
Correct |
294 ms |
56688 KB |
Output is correct |