#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
struct Tree {
vector<vector<int>> adj, lift;
vector<int> d, tin, tout;
int n, timer;
bool initialized = false;
bool dfsed = false;
void init(int nn){
n = nn;
adj.resize(n + 1);
d.resize(n + 1);
lift.resize(n + 1);
tin.resize(n + 1);
tout.resize(n + 1);
for (int i = 1; i <= n; i++) adj[i].clear();
for (int i = 0; i <= n; i++) lift[i].resize(20, 0);
initialized = true;
}
void addEdge(int u, int v){
if (!initialized){
cout << "STUPID INITIALIZE\n";
exit(0);
}
adj[u].push_back(v);
adj[v].push_back(u);
}
void build(){
for (int j = 1; j < 20; j++){
for (int i = 1; i <= n; i++){
lift[i][j] = lift[lift[i][j - 1]][j - 1];
}
}
}
void dfs(int u, int par){
tin[u] = ++timer;
for (int v : adj[u]){
if (v != par){
d[v] = d[u] + 1;
lift[v][0] = u;
dfs(v, u);
}
}
tout[u] = timer;
}
void dfs(int root = 1){
if (!initialized){
cout << "STUPID INITIALIZE\n";
exit(0);
}
d[root] = 0;
timer = 0;
dfs(root, -1);
build();
dfsed = true;
}
int lca(int a, int b){
if (!dfsed){
cout << "STUPID DFS\n";
exit(0);
}
if (d[a] < d[b]) swap(a, b);
int del = d[a] - d[b];
for (int i = 0; i < 20; i++) if (del >> i & 1) a = lift[a][i];
if (a == b) return a;
for (int i = 19; i >= 0; i--) if (lift[a][i] != lift[b][i]){
a = lift[a][i];
b = lift[b][i];
}
return lift[a][0];
}
int dist(int a, int b){
return d[a] + d[b] - 2 * d[lca(a, b)];
}
};
struct ufds{
vector <int> root, sz, mx;
int n;
void init(int nn){
n = nn;
root.resize(n + 1);
sz.resize(n + 1, 1);
mx.resize(n + 1);
for (int i = 1; i <= n; i++) root[i] = i, mx[i] = i;
}
int find(int x){
if (root[x] == x) return x;
return root[x] = find(root[x]);
}
bool unite(int x, int y){
x = find(x); y = find(y);
if (x == y) return false;
if (sz[y] > sz[x]) swap(x, y);
sz[x] += sz[y];
root[y] = x;
mx[x] = max(mx[x], mx[y]);
return true;
}
};
void Solve(){
int n; cin >> n;
vector <int> p(n + 1);
for (int i = 1; i <= n; i++) cin >> p[i];
Tree G;
G.init(n);
for (int i = 1; i < n; i++){
int u, v; cin >> u >> v;
G.addEdge(p[u], p[v]);
}
G.dfs();
vector <int> dp(n + 1, 0);
ufds uf;
uf.init(n);
for (int i = 2; i <= n; i++){
for (int v : G.adj[i]){
if (v < i){
int u = uf.mx[uf.find(v)];
dp[i] = max(dp[i], dp[u] + G.dist(u, i));
uf.unite(v, i);
}
}
}
cout << dp[n] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
3 ms |
2128 KB |
Output is correct |
20 |
Correct |
4 ms |
2132 KB |
Output is correct |
21 |
Correct |
3 ms |
2004 KB |
Output is correct |
22 |
Correct |
3 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
2000 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
3 ms |
2128 KB |
Output is correct |
20 |
Correct |
4 ms |
2132 KB |
Output is correct |
21 |
Correct |
3 ms |
2004 KB |
Output is correct |
22 |
Correct |
3 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
2000 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
2004 KB |
Output is correct |
27 |
Correct |
3 ms |
1976 KB |
Output is correct |
28 |
Correct |
3 ms |
1976 KB |
Output is correct |
29 |
Correct |
3 ms |
2004 KB |
Output is correct |
30 |
Correct |
3 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1952 KB |
Output is correct |
33 |
Correct |
3 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
3 ms |
2128 KB |
Output is correct |
20 |
Correct |
4 ms |
2132 KB |
Output is correct |
21 |
Correct |
3 ms |
2004 KB |
Output is correct |
22 |
Correct |
3 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
2000 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
168 ms |
66700 KB |
Output is correct |
26 |
Correct |
171 ms |
73544 KB |
Output is correct |
27 |
Correct |
170 ms |
73512 KB |
Output is correct |
28 |
Correct |
259 ms |
73176 KB |
Output is correct |
29 |
Correct |
264 ms |
70032 KB |
Output is correct |
30 |
Correct |
261 ms |
72300 KB |
Output is correct |
31 |
Correct |
283 ms |
71804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
238 ms |
64444 KB |
Output is correct |
4 |
Correct |
242 ms |
67204 KB |
Output is correct |
5 |
Correct |
244 ms |
67212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
3 ms |
2128 KB |
Output is correct |
20 |
Correct |
4 ms |
2132 KB |
Output is correct |
21 |
Correct |
3 ms |
2004 KB |
Output is correct |
22 |
Correct |
3 ms |
2004 KB |
Output is correct |
23 |
Correct |
3 ms |
2000 KB |
Output is correct |
24 |
Correct |
3 ms |
2004 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
2004 KB |
Output is correct |
27 |
Correct |
3 ms |
1976 KB |
Output is correct |
28 |
Correct |
3 ms |
1976 KB |
Output is correct |
29 |
Correct |
3 ms |
2004 KB |
Output is correct |
30 |
Correct |
3 ms |
1876 KB |
Output is correct |
31 |
Correct |
3 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1952 KB |
Output is correct |
33 |
Correct |
3 ms |
1876 KB |
Output is correct |
34 |
Correct |
168 ms |
66700 KB |
Output is correct |
35 |
Correct |
171 ms |
73544 KB |
Output is correct |
36 |
Correct |
170 ms |
73512 KB |
Output is correct |
37 |
Correct |
259 ms |
73176 KB |
Output is correct |
38 |
Correct |
264 ms |
70032 KB |
Output is correct |
39 |
Correct |
261 ms |
72300 KB |
Output is correct |
40 |
Correct |
283 ms |
71804 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
238 ms |
64444 KB |
Output is correct |
44 |
Correct |
242 ms |
67204 KB |
Output is correct |
45 |
Correct |
244 ms |
67212 KB |
Output is correct |
46 |
Correct |
202 ms |
72044 KB |
Output is correct |
47 |
Correct |
208 ms |
72088 KB |
Output is correct |
48 |
Correct |
193 ms |
72012 KB |
Output is correct |
49 |
Correct |
192 ms |
72000 KB |
Output is correct |
50 |
Correct |
291 ms |
67736 KB |
Output is correct |
51 |
Correct |
283 ms |
67712 KB |
Output is correct |
52 |
Correct |
281 ms |
67720 KB |
Output is correct |
53 |
Correct |
295 ms |
67708 KB |
Output is correct |