#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#ifndef LOCAL
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
mt19937 mt(123);
void solve();
void init();
int32_t main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cout << fixed << setprecision(30);
init();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
void init() {}
const int LG = 18, N = 1 << LG;
int p[N], dsu[N], tin[N], tout[N], timer;
ll dp[N];
pair<int, int> binup[LG][N];
vector<int> g[N];
int get(int x) {
return x == dsu[x] ? x : dsu[x] = get(dsu[x]);
}
void merge(int x, int y) {
x = get(x);
y = get(y);
if (x < y) {
swap(x, y);
}
dsu[y] = x;
}
void dfs(int s, int p = -1) {
tin[s] = timer++;
if (p == -1) {
binup[0][s] = {0, s};
} else {
binup[0][s] = {1, p};
}
for (int i : g[s]) {
if (i != p) {
dfs(i, s);
}
}
tout[s] = timer++;
}
bool check_pr(int i, int p) {
return tin[p] <= tin[i] && tin[i] <= tout[p];
}
int get_dist(int u, int v) {
int nw = u, res = 0;
for (int l = LG - 1; l >= 0; l--) {
if (!check_pr(v, binup[l][nw].y)) {
res += binup[l][nw].x;
nw = binup[l][nw].y;
}
}
if (!check_pr(v, nw)) {
res++;
}
nw = v;
for (int l = LG - 1; l >= 0; l--) {
if (!check_pr(u, binup[l][nw].y)) {
res += binup[l][nw].x;
nw = binup[l][nw].y;
}
}
if (!check_pr(u, nw)) {
res++;
}
return res;
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i]; p[i]--;
dsu[i] = i;
dp[i] = 0;
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v; u--; v--;
u = p[u];
v = p[v];
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
for (int l = 1; l < LG; l++) {
for (int i = 0; i < n; i++) {
binup[l][i] = {binup[l - 1][i].x + binup[l - 1][binup[l - 1][i].y].x, binup[l - 1][binup[l - 1][i].y].y};
}
}
for (int i = 0; i < n; i++) {
for (int j : g[i]) {
if (j < i) {
dp[i] = max(dp[i], dp[get(j)] + get_dist(i, get(j)));
}
}
for (int j : g[i]) {
if (j < i) {
merge(i, j);
}
}
}
cout << dp[n - 1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43608 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
6 ms |
45660 KB |
Output is correct |
4 |
Correct |
6 ms |
45660 KB |
Output is correct |
5 |
Correct |
6 ms |
45660 KB |
Output is correct |
6 |
Correct |
6 ms |
43612 KB |
Output is correct |
7 |
Correct |
6 ms |
45660 KB |
Output is correct |
8 |
Correct |
6 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45912 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43608 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
6 ms |
45660 KB |
Output is correct |
4 |
Correct |
6 ms |
45660 KB |
Output is correct |
5 |
Correct |
6 ms |
45660 KB |
Output is correct |
6 |
Correct |
6 ms |
43612 KB |
Output is correct |
7 |
Correct |
6 ms |
45660 KB |
Output is correct |
8 |
Correct |
6 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45912 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
6 ms |
45660 KB |
Output is correct |
12 |
Correct |
6 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
45660 KB |
Output is correct |
14 |
Correct |
6 ms |
43612 KB |
Output is correct |
15 |
Correct |
4 ms |
43612 KB |
Output is correct |
16 |
Correct |
7 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
45660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43608 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
6 ms |
45660 KB |
Output is correct |
4 |
Correct |
6 ms |
45660 KB |
Output is correct |
5 |
Correct |
6 ms |
45660 KB |
Output is correct |
6 |
Correct |
6 ms |
43612 KB |
Output is correct |
7 |
Correct |
6 ms |
45660 KB |
Output is correct |
8 |
Correct |
6 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45912 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
6 ms |
45660 KB |
Output is correct |
12 |
Correct |
6 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
45660 KB |
Output is correct |
14 |
Correct |
6 ms |
43612 KB |
Output is correct |
15 |
Correct |
4 ms |
43612 KB |
Output is correct |
16 |
Correct |
7 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
45660 KB |
Output is correct |
18 |
Correct |
8 ms |
46172 KB |
Output is correct |
19 |
Correct |
8 ms |
44304 KB |
Output is correct |
20 |
Correct |
8 ms |
42136 KB |
Output is correct |
21 |
Correct |
9 ms |
40028 KB |
Output is correct |
22 |
Correct |
8 ms |
42332 KB |
Output is correct |
23 |
Correct |
8 ms |
42256 KB |
Output is correct |
24 |
Correct |
8 ms |
42224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43608 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
6 ms |
45660 KB |
Output is correct |
4 |
Correct |
6 ms |
45660 KB |
Output is correct |
5 |
Correct |
6 ms |
45660 KB |
Output is correct |
6 |
Correct |
6 ms |
43612 KB |
Output is correct |
7 |
Correct |
6 ms |
45660 KB |
Output is correct |
8 |
Correct |
6 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45912 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
6 ms |
45660 KB |
Output is correct |
12 |
Correct |
6 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
45660 KB |
Output is correct |
14 |
Correct |
6 ms |
43612 KB |
Output is correct |
15 |
Correct |
4 ms |
43612 KB |
Output is correct |
16 |
Correct |
7 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
45660 KB |
Output is correct |
18 |
Correct |
8 ms |
46172 KB |
Output is correct |
19 |
Correct |
8 ms |
44304 KB |
Output is correct |
20 |
Correct |
8 ms |
42136 KB |
Output is correct |
21 |
Correct |
9 ms |
40028 KB |
Output is correct |
22 |
Correct |
8 ms |
42332 KB |
Output is correct |
23 |
Correct |
8 ms |
42256 KB |
Output is correct |
24 |
Correct |
8 ms |
42224 KB |
Output is correct |
25 |
Correct |
6 ms |
41560 KB |
Output is correct |
26 |
Correct |
8 ms |
40024 KB |
Output is correct |
27 |
Correct |
8 ms |
42072 KB |
Output is correct |
28 |
Correct |
8 ms |
42264 KB |
Output is correct |
29 |
Correct |
8 ms |
40028 KB |
Output is correct |
30 |
Correct |
8 ms |
41820 KB |
Output is correct |
31 |
Correct |
8 ms |
40028 KB |
Output is correct |
32 |
Correct |
9 ms |
41820 KB |
Output is correct |
33 |
Correct |
8 ms |
41820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43608 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
6 ms |
45660 KB |
Output is correct |
4 |
Correct |
6 ms |
45660 KB |
Output is correct |
5 |
Correct |
6 ms |
45660 KB |
Output is correct |
6 |
Correct |
6 ms |
43612 KB |
Output is correct |
7 |
Correct |
6 ms |
45660 KB |
Output is correct |
8 |
Correct |
6 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45912 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
6 ms |
45660 KB |
Output is correct |
12 |
Correct |
6 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
45660 KB |
Output is correct |
14 |
Correct |
6 ms |
43612 KB |
Output is correct |
15 |
Correct |
4 ms |
43612 KB |
Output is correct |
16 |
Correct |
7 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
45660 KB |
Output is correct |
18 |
Correct |
8 ms |
46172 KB |
Output is correct |
19 |
Correct |
8 ms |
44304 KB |
Output is correct |
20 |
Correct |
8 ms |
42136 KB |
Output is correct |
21 |
Correct |
9 ms |
40028 KB |
Output is correct |
22 |
Correct |
8 ms |
42332 KB |
Output is correct |
23 |
Correct |
8 ms |
42256 KB |
Output is correct |
24 |
Correct |
8 ms |
42224 KB |
Output is correct |
25 |
Correct |
104 ms |
62984 KB |
Output is correct |
26 |
Correct |
98 ms |
69756 KB |
Output is correct |
27 |
Correct |
108 ms |
69572 KB |
Output is correct |
28 |
Correct |
164 ms |
69076 KB |
Output is correct |
29 |
Correct |
192 ms |
63488 KB |
Output is correct |
30 |
Correct |
163 ms |
68112 KB |
Output is correct |
31 |
Correct |
176 ms |
66932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
45660 KB |
Output is correct |
2 |
Correct |
6 ms |
45808 KB |
Output is correct |
3 |
Correct |
159 ms |
58140 KB |
Output is correct |
4 |
Correct |
172 ms |
58308 KB |
Output is correct |
5 |
Correct |
165 ms |
58140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
43608 KB |
Output is correct |
2 |
Correct |
6 ms |
45660 KB |
Output is correct |
3 |
Correct |
6 ms |
45660 KB |
Output is correct |
4 |
Correct |
6 ms |
45660 KB |
Output is correct |
5 |
Correct |
6 ms |
45660 KB |
Output is correct |
6 |
Correct |
6 ms |
43612 KB |
Output is correct |
7 |
Correct |
6 ms |
45660 KB |
Output is correct |
8 |
Correct |
6 ms |
45660 KB |
Output is correct |
9 |
Correct |
7 ms |
45912 KB |
Output is correct |
10 |
Correct |
6 ms |
43612 KB |
Output is correct |
11 |
Correct |
6 ms |
45660 KB |
Output is correct |
12 |
Correct |
6 ms |
43612 KB |
Output is correct |
13 |
Correct |
6 ms |
45660 KB |
Output is correct |
14 |
Correct |
6 ms |
43612 KB |
Output is correct |
15 |
Correct |
4 ms |
43612 KB |
Output is correct |
16 |
Correct |
7 ms |
43608 KB |
Output is correct |
17 |
Correct |
6 ms |
45660 KB |
Output is correct |
18 |
Correct |
8 ms |
46172 KB |
Output is correct |
19 |
Correct |
8 ms |
44304 KB |
Output is correct |
20 |
Correct |
8 ms |
42136 KB |
Output is correct |
21 |
Correct |
9 ms |
40028 KB |
Output is correct |
22 |
Correct |
8 ms |
42332 KB |
Output is correct |
23 |
Correct |
8 ms |
42256 KB |
Output is correct |
24 |
Correct |
8 ms |
42224 KB |
Output is correct |
25 |
Correct |
6 ms |
41560 KB |
Output is correct |
26 |
Correct |
8 ms |
40024 KB |
Output is correct |
27 |
Correct |
8 ms |
42072 KB |
Output is correct |
28 |
Correct |
8 ms |
42264 KB |
Output is correct |
29 |
Correct |
8 ms |
40028 KB |
Output is correct |
30 |
Correct |
8 ms |
41820 KB |
Output is correct |
31 |
Correct |
8 ms |
40028 KB |
Output is correct |
32 |
Correct |
9 ms |
41820 KB |
Output is correct |
33 |
Correct |
8 ms |
41820 KB |
Output is correct |
34 |
Correct |
104 ms |
62984 KB |
Output is correct |
35 |
Correct |
98 ms |
69756 KB |
Output is correct |
36 |
Correct |
108 ms |
69572 KB |
Output is correct |
37 |
Correct |
164 ms |
69076 KB |
Output is correct |
38 |
Correct |
192 ms |
63488 KB |
Output is correct |
39 |
Correct |
163 ms |
68112 KB |
Output is correct |
40 |
Correct |
176 ms |
66932 KB |
Output is correct |
41 |
Correct |
6 ms |
45660 KB |
Output is correct |
42 |
Correct |
6 ms |
45808 KB |
Output is correct |
43 |
Correct |
159 ms |
58140 KB |
Output is correct |
44 |
Correct |
172 ms |
58308 KB |
Output is correct |
45 |
Correct |
165 ms |
58140 KB |
Output is correct |
46 |
Correct |
123 ms |
66524 KB |
Output is correct |
47 |
Correct |
114 ms |
66388 KB |
Output is correct |
48 |
Correct |
122 ms |
66384 KB |
Output is correct |
49 |
Correct |
117 ms |
66384 KB |
Output is correct |
50 |
Correct |
200 ms |
57752 KB |
Output is correct |
51 |
Correct |
177 ms |
58192 KB |
Output is correct |
52 |
Correct |
176 ms |
57680 KB |
Output is correct |
53 |
Correct |
173 ms |
58196 KB |
Output is correct |