#include <bits/stdc++.h>
using namespace std;
int n, val[210000], fa[210000], vtoi[210000], par[210000][21], dep[210000] = {0};
long long ans[210000] = {0};
vector <int> e[210000];
int find (int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}
void dfs(int i, int p) {
dep[i] = dep[p] + 1;
par[i][0] = p;
for (int j : e[i]) if (j != p) dfs(j, i);
}
int dis (int x, int y) {
int res = 0;
if (dep[x] > dep[y]) swap(x, y);
for (int i = 0; i <= 19; i++) if ((dep[y] - dep[x]) & (1 << i)) y = par[y][i], res += (1 << i);
if (x == y) return res;
for (int j = 19; j >= 0; j--) if (par[x][j] != par[y][j]) x = par[x][j], y = par[y][j], res += (1 << j) << 1;
res += 2;
return res;}
int main () {
//輸入、建圖
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
vtoi[val[i]] = i, fa[i] = i;
}
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
//初始化lca
dfs(1, 1);
for (int j = 1; j <= 19; j++) for (int i = 1; i <= n; i++) par[i][j] = par[par[i][j - 1]][j - 1];
//做事(反著作回來,原本的操作是拔值最大的點,然後選一個子樹進去做一樣的事。現在反著做就變加點了)
for (int ii = 1; ii <= n; ii++) {
int i = vtoi[ii];
for (int j : e[i]) {
if (val[j] > ii) continue;
j = find(j);
ans[i] = max(ans[i], ans[j] + dis(j, i));
fa[j] = i;
}
}
cout << ans[vtoi[n]] << '\n';
return 0;}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10892 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10892 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10892 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10888 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10892 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10892 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10888 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11608 KB |
Output is correct |
19 |
Correct |
4 ms |
11608 KB |
Output is correct |
20 |
Correct |
4 ms |
11612 KB |
Output is correct |
21 |
Correct |
4 ms |
11612 KB |
Output is correct |
22 |
Correct |
4 ms |
11612 KB |
Output is correct |
23 |
Correct |
4 ms |
11612 KB |
Output is correct |
24 |
Correct |
4 ms |
11612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10892 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10892 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10888 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11608 KB |
Output is correct |
19 |
Correct |
4 ms |
11608 KB |
Output is correct |
20 |
Correct |
4 ms |
11612 KB |
Output is correct |
21 |
Correct |
4 ms |
11612 KB |
Output is correct |
22 |
Correct |
4 ms |
11612 KB |
Output is correct |
23 |
Correct |
4 ms |
11612 KB |
Output is correct |
24 |
Correct |
4 ms |
11612 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
4 ms |
11696 KB |
Output is correct |
27 |
Correct |
4 ms |
11612 KB |
Output is correct |
28 |
Correct |
4 ms |
11676 KB |
Output is correct |
29 |
Correct |
4 ms |
11612 KB |
Output is correct |
30 |
Correct |
4 ms |
11356 KB |
Output is correct |
31 |
Correct |
4 ms |
11356 KB |
Output is correct |
32 |
Correct |
4 ms |
11356 KB |
Output is correct |
33 |
Correct |
5 ms |
11608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10892 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10892 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10888 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11608 KB |
Output is correct |
19 |
Correct |
4 ms |
11608 KB |
Output is correct |
20 |
Correct |
4 ms |
11612 KB |
Output is correct |
21 |
Correct |
4 ms |
11612 KB |
Output is correct |
22 |
Correct |
4 ms |
11612 KB |
Output is correct |
23 |
Correct |
4 ms |
11612 KB |
Output is correct |
24 |
Correct |
4 ms |
11612 KB |
Output is correct |
25 |
Correct |
101 ms |
46864 KB |
Output is correct |
26 |
Correct |
92 ms |
46672 KB |
Output is correct |
27 |
Correct |
91 ms |
46680 KB |
Output is correct |
28 |
Correct |
115 ms |
46676 KB |
Output is correct |
29 |
Correct |
123 ms |
46852 KB |
Output is correct |
30 |
Correct |
125 ms |
46720 KB |
Output is correct |
31 |
Correct |
115 ms |
46672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
137 ms |
37352 KB |
Output is correct |
4 |
Correct |
131 ms |
37332 KB |
Output is correct |
5 |
Correct |
130 ms |
37352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10840 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10892 KB |
Output is correct |
7 |
Correct |
3 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10892 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10888 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
11608 KB |
Output is correct |
19 |
Correct |
4 ms |
11608 KB |
Output is correct |
20 |
Correct |
4 ms |
11612 KB |
Output is correct |
21 |
Correct |
4 ms |
11612 KB |
Output is correct |
22 |
Correct |
4 ms |
11612 KB |
Output is correct |
23 |
Correct |
4 ms |
11612 KB |
Output is correct |
24 |
Correct |
4 ms |
11612 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
4 ms |
11696 KB |
Output is correct |
27 |
Correct |
4 ms |
11612 KB |
Output is correct |
28 |
Correct |
4 ms |
11676 KB |
Output is correct |
29 |
Correct |
4 ms |
11612 KB |
Output is correct |
30 |
Correct |
4 ms |
11356 KB |
Output is correct |
31 |
Correct |
4 ms |
11356 KB |
Output is correct |
32 |
Correct |
4 ms |
11356 KB |
Output is correct |
33 |
Correct |
5 ms |
11608 KB |
Output is correct |
34 |
Correct |
101 ms |
46864 KB |
Output is correct |
35 |
Correct |
92 ms |
46672 KB |
Output is correct |
36 |
Correct |
91 ms |
46680 KB |
Output is correct |
37 |
Correct |
115 ms |
46676 KB |
Output is correct |
38 |
Correct |
123 ms |
46852 KB |
Output is correct |
39 |
Correct |
125 ms |
46720 KB |
Output is correct |
40 |
Correct |
115 ms |
46672 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
137 ms |
37352 KB |
Output is correct |
44 |
Correct |
131 ms |
37332 KB |
Output is correct |
45 |
Correct |
130 ms |
37352 KB |
Output is correct |
46 |
Correct |
176 ms |
42480 KB |
Output is correct |
47 |
Correct |
203 ms |
41812 KB |
Output is correct |
48 |
Correct |
172 ms |
42892 KB |
Output is correct |
49 |
Correct |
186 ms |
41808 KB |
Output is correct |
50 |
Correct |
164 ms |
37460 KB |
Output is correct |
51 |
Correct |
165 ms |
37528 KB |
Output is correct |
52 |
Correct |
169 ms |
37456 KB |
Output is correct |
53 |
Correct |
184 ms |
37828 KB |
Output is correct |