#include <bits/stdc++.h>
using namespace std;
vector<int> h(200001);
vector<int> haha[200001];
vector<int> dsu(200001);
vector<int> dep(200001);
int bruh[200001][19];
void dfs(int a, int t, int d) {
bruh[a][0] = t;
dep[a] = d;
for(int v: haha[a]) {
if(v != t) {
dfs(v,a,d+1);
}
}
}
int check(int a) {
int 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;
}
int dist(int a, int b) {
if(dep[a] < dep[b]) {
swap(a,b);
}
int c = dep[a]-dep[b],e = a;
for(int i = 0; i < 19; i++) {
if((1 << i)&c) {
e = bruh[e][i];
}
}
if(b != e) {
for(int 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[b]-dep[e];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for(int i = 1; i <= 200000; i++) {
dsu[i] = i;
}
int n,a,b,y = 0;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
vector<pair<int,pair<int,int>>> edge(0);
for(int 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(int i = 1; i < 19; i++) {
for(int 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<int> ans(n+1);
for(int 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(int i = 1; i <= n; i++) {
if(h[i] == n) {
cout << ans[i];
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7284 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7284 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7392 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7392 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7392 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7284 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7392 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7392 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7392 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8252 KB |
Output is correct |
19 |
Correct |
7 ms |
8276 KB |
Output is correct |
20 |
Correct |
6 ms |
8276 KB |
Output is correct |
21 |
Correct |
6 ms |
8300 KB |
Output is correct |
22 |
Correct |
6 ms |
8276 KB |
Output is correct |
23 |
Correct |
7 ms |
8276 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7284 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7392 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7392 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7392 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8252 KB |
Output is correct |
19 |
Correct |
7 ms |
8276 KB |
Output is correct |
20 |
Correct |
6 ms |
8276 KB |
Output is correct |
21 |
Correct |
6 ms |
8300 KB |
Output is correct |
22 |
Correct |
6 ms |
8276 KB |
Output is correct |
23 |
Correct |
7 ms |
8276 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
25 |
Correct |
3 ms |
7380 KB |
Output is correct |
26 |
Incorrect |
6 ms |
8100 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7284 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7392 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7392 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7392 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8252 KB |
Output is correct |
19 |
Correct |
7 ms |
8276 KB |
Output is correct |
20 |
Correct |
6 ms |
8276 KB |
Output is correct |
21 |
Correct |
6 ms |
8300 KB |
Output is correct |
22 |
Correct |
6 ms |
8276 KB |
Output is correct |
23 |
Correct |
7 ms |
8276 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
25 |
Incorrect |
150 ms |
44796 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7400 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7284 KB |
Output is correct |
3 |
Correct |
4 ms |
7396 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7392 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7392 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7392 KB |
Output is correct |
16 |
Correct |
3 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8252 KB |
Output is correct |
19 |
Correct |
7 ms |
8276 KB |
Output is correct |
20 |
Correct |
6 ms |
8276 KB |
Output is correct |
21 |
Correct |
6 ms |
8300 KB |
Output is correct |
22 |
Correct |
6 ms |
8276 KB |
Output is correct |
23 |
Correct |
7 ms |
8276 KB |
Output is correct |
24 |
Correct |
6 ms |
8276 KB |
Output is correct |
25 |
Correct |
3 ms |
7380 KB |
Output is correct |
26 |
Incorrect |
6 ms |
8100 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |