#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
vector<int> adj[N];
vector<int> depth(N);
int parents[N][25];
vector<int> parent(N);
vector<int> maxVal(N);
vector<int> h(N);
vector<int> sz(N);
void dfs(int u, int p){
for(int v : adj[u]){
if(v != p){
parents[v][0] = u;
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
}
int distance(int a, int b){
int dist = depth[a] + depth[b];
if(depth[a] < depth[b]){
swap(a, b);
}
int diff = depth[a] - depth[b];
int val = 0;
while(diff > 0){
if(diff & 1){
a = parents[a][val];
}
diff /= 2;
val++;
}
if(a == b){
return dist - 2 * depth[a];
}
for(int i = 24; i >= 0; i++){
if(parents[a][i] != parents[b][i]){
a = parents[a][i];
b = parents[b][i];
}
}
return dist - 2 * depth[a] + 2;
}
int getRoot(int u){
if(parent[u] == u){
return u;
}
return parent[u] = getRoot(parent[u]);
}
void unite(int a, int b){
int u = getRoot(a);
int v = getRoot(b);
if(u == v){
return;
}
if(sz[u] == sz[v]){
sz[u]++;
}
if(sz[u] > sz[v]){
parent[v] = u;
if(h[maxVal[v]] > h[maxVal[u]]){
maxVal[u] = maxVal[v];
}
}
else{
parent[u] = v;
if(h[maxVal[u]] > h[maxVal[v]]){
maxVal[v] = maxVal[u];
}
}
}
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> h[i];
}
for(int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
parents[1][0] = 1;
dfs(1, 0);
for(int j = 1; j <= 18; j++){
for(int i = 1; i <= n; i++){
parents[i][j] = parents[parents[i][j - 1]][j - 1];
}
}
for(int i = 1; i <= n; i++){
parent[i] = i;
maxVal[i] = i;
}
vector<pair<int, int> > adj2[1 + n];
vector<pair<int, int> > positions(1 + n);
for(int i = 1; i <= n; i++){
positions[i] = make_pair(h[i], i);
}
sort(positions.begin(), positions.end());
for(int i = 1; i <= n; i++){
int u = positions[i].second;
for(int v : adj[u]){
if(h[v] <= h[u]){
int weight = maxVal[getRoot(v)];
adj2[u].push_back(make_pair(weight, distance(u, weight)));
unite(u, v);
}
}
}
vector<int> dist(1 + n);
vector<bool> visited(1 + n, false);
queue<int> q;
q.push(positions[n].second);
while(!q.empty()){
int u = q.front();
q.pop();
visited[u] = true;
for(pair<int, int> x : adj2[u]){
int v = x.first;
if(!visited[v]){
visited[v] = true;
dist[v] = dist[u] + x.second;
q.push(v);
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
ans = max(ans, dist[i]);
}
cout << ans << "\n";
}
Compilation message
Main.cpp: In function 'int distance(int, int)':
Main.cpp:37:5: warning: iteration 2147483623 invokes undefined behavior [-Waggressive-loop-optimizations]
37 | for(int i = 24; i >= 0; i++){
| ^~~
Main.cpp:37:23: note: within this loop
37 | for(int i = 24; i >= 0; i++){
| ~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
3 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
10072 KB |
Output is correct |
6 |
Correct |
3 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
3 ms |
9816 KB |
Output is correct |
10 |
Correct |
3 ms |
9816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
3 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
10072 KB |
Output is correct |
6 |
Correct |
3 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
3 ms |
9816 KB |
Output is correct |
10 |
Correct |
3 ms |
9816 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9816 KB |
Output is correct |
16 |
Correct |
3 ms |
9816 KB |
Output is correct |
17 |
Correct |
4 ms |
10004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
3 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
10072 KB |
Output is correct |
6 |
Correct |
3 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
3 ms |
9816 KB |
Output is correct |
10 |
Correct |
3 ms |
9816 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9816 KB |
Output is correct |
16 |
Correct |
3 ms |
9816 KB |
Output is correct |
17 |
Correct |
4 ms |
10004 KB |
Output is correct |
18 |
Correct |
8 ms |
13144 KB |
Output is correct |
19 |
Correct |
7 ms |
12892 KB |
Output is correct |
20 |
Correct |
8 ms |
12888 KB |
Output is correct |
21 |
Correct |
8 ms |
12888 KB |
Output is correct |
22 |
Correct |
10 ms |
12888 KB |
Output is correct |
23 |
Correct |
8 ms |
12888 KB |
Output is correct |
24 |
Correct |
7 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
3 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
10072 KB |
Output is correct |
6 |
Correct |
3 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
3 ms |
9816 KB |
Output is correct |
10 |
Correct |
3 ms |
9816 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9816 KB |
Output is correct |
16 |
Correct |
3 ms |
9816 KB |
Output is correct |
17 |
Correct |
4 ms |
10004 KB |
Output is correct |
18 |
Correct |
8 ms |
13144 KB |
Output is correct |
19 |
Correct |
7 ms |
12892 KB |
Output is correct |
20 |
Correct |
8 ms |
12888 KB |
Output is correct |
21 |
Correct |
8 ms |
12888 KB |
Output is correct |
22 |
Correct |
10 ms |
12888 KB |
Output is correct |
23 |
Correct |
8 ms |
12888 KB |
Output is correct |
24 |
Correct |
7 ms |
12888 KB |
Output is correct |
25 |
Execution timed out |
2045 ms |
9816 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
3 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
10072 KB |
Output is correct |
6 |
Correct |
3 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
3 ms |
9816 KB |
Output is correct |
10 |
Correct |
3 ms |
9816 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9816 KB |
Output is correct |
16 |
Correct |
3 ms |
9816 KB |
Output is correct |
17 |
Correct |
4 ms |
10004 KB |
Output is correct |
18 |
Correct |
8 ms |
13144 KB |
Output is correct |
19 |
Correct |
7 ms |
12892 KB |
Output is correct |
20 |
Correct |
8 ms |
12888 KB |
Output is correct |
21 |
Correct |
8 ms |
12888 KB |
Output is correct |
22 |
Correct |
10 ms |
12888 KB |
Output is correct |
23 |
Correct |
8 ms |
12888 KB |
Output is correct |
24 |
Correct |
7 ms |
12888 KB |
Output is correct |
25 |
Incorrect |
228 ms |
59728 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2025 ms |
9816 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9816 KB |
Output is correct |
3 |
Correct |
3 ms |
9816 KB |
Output is correct |
4 |
Correct |
3 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
10072 KB |
Output is correct |
6 |
Correct |
3 ms |
10072 KB |
Output is correct |
7 |
Correct |
4 ms |
10076 KB |
Output is correct |
8 |
Correct |
3 ms |
9816 KB |
Output is correct |
9 |
Correct |
3 ms |
9816 KB |
Output is correct |
10 |
Correct |
3 ms |
9816 KB |
Output is correct |
11 |
Correct |
3 ms |
10072 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9816 KB |
Output is correct |
16 |
Correct |
3 ms |
9816 KB |
Output is correct |
17 |
Correct |
4 ms |
10004 KB |
Output is correct |
18 |
Correct |
8 ms |
13144 KB |
Output is correct |
19 |
Correct |
7 ms |
12892 KB |
Output is correct |
20 |
Correct |
8 ms |
12888 KB |
Output is correct |
21 |
Correct |
8 ms |
12888 KB |
Output is correct |
22 |
Correct |
10 ms |
12888 KB |
Output is correct |
23 |
Correct |
8 ms |
12888 KB |
Output is correct |
24 |
Correct |
7 ms |
12888 KB |
Output is correct |
25 |
Execution timed out |
2045 ms |
9816 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |