#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
#define int long long
const int lg = 18;
struct DSU {
vector<int> parent;
DSU() {}
DSU(int N): parent(N, -1) {}
int get(int x){
return (parent[x] < 0) ? x : (parent[x] = get(parent[x]));
}
bool merge(int a, int b){
a = get(a); b = get(b);
if (a == b) return false;
if (parent[a] > parent[b]) swap(a, b);
parent[a] += parent[b];
parent[b] = a;
return true;
}
};
int n;
vector<int> p;
vector<vector<int>> adj;
vector<int> best, dp;
vector<vector<int>> par;
vector<int> depth;
void dfs(int x, int P, int d = 0){
par[0][x] = P;
depth[x] = d;
for (auto k: adj[x]){
if (k == P) continue;
dfs(k, x, d+1);
}
}
void build(){
par.resize(lg, vector<int>(n));
depth.resize(n);
dfs(0, 0);
for (int j = 1; j < lg; j++){
for (int i = 0; i < n; i++){
par[j][i] = par[j-1][par[j-1][i]];
}
}
}
int dist(int x, int y){
int sum = 0;
if (depth[x] > depth[y]) swap(x, y);
for (int j = 0; j < lg; j++){
if (((depth[y] - depth[x]) >> j) & 1){
sum += (1 << j);
y = par[j][y];
}
}
if (x == y) return sum;
for (int j = lg-1; j >= 0; j--){
if (par[j][x] != par[j][y]){
sum += (1 << (j + 1));
x = par[j][x];
y = par[j][y];
}
}
return sum + 2;
}
void main_program(){
cin >> n;
p.resize(n);
adj.resize(n);
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < n-1; i++){
int x, y; cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
build();
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int x, int y){
return p[x] < p[y];
});
best.resize(n);
iota(best.begin(), best.end(), 0);
dp.resize(n);
DSU dsu(n);
for (auto x: order){
for (auto y: adj[x]){
if (p[x] < p[y]) continue;
int old_root = dsu.get(y);
int old_best = best[old_root];
dp[x] = max(dp[x], dp[old_best] + dist(old_best, x));
dsu.merge(x, y);
int root = dsu.get(x);
best[root] = x;
}
}
cout << dp[order.back()] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
2136 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
2136 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1628 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
4 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1628 KB |
Output is correct |
33 |
Correct |
3 ms |
1576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
2136 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
125 ms |
58272 KB |
Output is correct |
26 |
Correct |
106 ms |
62004 KB |
Output is correct |
27 |
Correct |
105 ms |
61996 KB |
Output is correct |
28 |
Correct |
148 ms |
62108 KB |
Output is correct |
29 |
Correct |
161 ms |
62008 KB |
Output is correct |
30 |
Correct |
169 ms |
62176 KB |
Output is correct |
31 |
Correct |
174 ms |
62188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
179 ms |
50472 KB |
Output is correct |
4 |
Correct |
203 ms |
50428 KB |
Output is correct |
5 |
Correct |
188 ms |
50564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
3 ms |
1884 KB |
Output is correct |
20 |
Correct |
3 ms |
1884 KB |
Output is correct |
21 |
Correct |
3 ms |
2136 KB |
Output is correct |
22 |
Correct |
3 ms |
1884 KB |
Output is correct |
23 |
Correct |
3 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
1884 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1628 KB |
Output is correct |
27 |
Correct |
3 ms |
1628 KB |
Output is correct |
28 |
Correct |
4 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1628 KB |
Output is correct |
33 |
Correct |
3 ms |
1576 KB |
Output is correct |
34 |
Correct |
125 ms |
58272 KB |
Output is correct |
35 |
Correct |
106 ms |
62004 KB |
Output is correct |
36 |
Correct |
105 ms |
61996 KB |
Output is correct |
37 |
Correct |
148 ms |
62108 KB |
Output is correct |
38 |
Correct |
161 ms |
62008 KB |
Output is correct |
39 |
Correct |
169 ms |
62176 KB |
Output is correct |
40 |
Correct |
174 ms |
62188 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
179 ms |
50472 KB |
Output is correct |
44 |
Correct |
203 ms |
50428 KB |
Output is correct |
45 |
Correct |
188 ms |
50564 KB |
Output is correct |
46 |
Correct |
237 ms |
58476 KB |
Output is correct |
47 |
Correct |
229 ms |
57484 KB |
Output is correct |
48 |
Correct |
239 ms |
58880 KB |
Output is correct |
49 |
Correct |
229 ms |
57400 KB |
Output is correct |
50 |
Correct |
228 ms |
53660 KB |
Output is correct |
51 |
Correct |
233 ms |
53832 KB |
Output is correct |
52 |
Correct |
229 ms |
53812 KB |
Output is correct |
53 |
Correct |
244 ms |
54068 KB |
Output is correct |