#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define MX (int)3e5+5
#define LOGM (int)18
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
vector<int> pa(MX, -1);
vector<int> woke(MX);
int leader(int u)
{
if(pa[u] < 0)
return u;
else
return pa[u] = leader(pa[u]);
}
void merge(int u, int v)
{
u = leader(u);
v = leader(v);
if(u==v)
return;
if(pa[u] < pa[v])
swap(u, v);
pa[v]+=pa[u];
woke[v] = max(woke[v], woke[u]);
pa[u] = v;
return;
}
vector<int> adj[MX];
int timer;
int lca[LOGM][MX];
vector<int> st(MX), en(MX), dep(MX);
void DFS(int u, int p, int lvl)
{
lca[0][u] = p;
st[u] = timer++;
dep[u] = lvl;
for(int v: adj[u])
{
if(v==p)
continue;
DFS(v, u, lvl+1);
}
en[u] = timer;
}
int lcaa(int u, int v)
{
if(dep[u] > dep[v])
swap(u, v);
if((st[u] <= st[v]) && (en[v] <= en[u]))
return u;
for(int i = LOGM-1; i >= 0; i--)
{
int k = lca[i][u];
if((st[k] <= st[v]) && (en[v] <= en[k]))
continue;
u = k;
}
return lca[0][u];
}
int dist(int u, int v)
{
return dep[u]+dep[v]-2*dep[lcaa(u, v)];
}
signed main()
{
fast();
int n;
cin >> n;
vector<int> a(n+1);
for(int i = 1; i <= n; i++)
cin >> a[i];
int m = n-1;
while(m--)
{
int u, v;
cin >> u >> v;
adj[a[u]].pb(a[v]);
adj[a[v]].pb(a[u]);
}
DFS(1, 1, 0);
for(int i = 1; i < LOGM; i++)
{
for(int u = 1; u <= n; u++)
lca[i][u] = lca[i-1][lca[i-1][u]];
}
vector<int> dp(n+1, 0);
woke[1] = 1;
for(int u = 2; u <= n; u++)
{
woke[u] = u;
for(int v: adj[u])
{
if(v > u)
continue;
dp[u] = max(dp[u], dp[woke[leader(v)]]+dist(u, woke[leader(v)]));
merge(u, v);
}
}
cout << dp[n] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19156 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
8 ms |
19180 KB |
Output is correct |
4 |
Correct |
8 ms |
19156 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
9 ms |
19200 KB |
Output is correct |
7 |
Correct |
8 ms |
19240 KB |
Output is correct |
8 |
Correct |
8 ms |
19248 KB |
Output is correct |
9 |
Correct |
8 ms |
19244 KB |
Output is correct |
10 |
Correct |
8 ms |
19176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19156 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
8 ms |
19180 KB |
Output is correct |
4 |
Correct |
8 ms |
19156 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
9 ms |
19200 KB |
Output is correct |
7 |
Correct |
8 ms |
19240 KB |
Output is correct |
8 |
Correct |
8 ms |
19248 KB |
Output is correct |
9 |
Correct |
8 ms |
19244 KB |
Output is correct |
10 |
Correct |
8 ms |
19176 KB |
Output is correct |
11 |
Correct |
8 ms |
19284 KB |
Output is correct |
12 |
Correct |
9 ms |
19312 KB |
Output is correct |
13 |
Correct |
9 ms |
19296 KB |
Output is correct |
14 |
Correct |
9 ms |
19252 KB |
Output is correct |
15 |
Correct |
8 ms |
19284 KB |
Output is correct |
16 |
Correct |
9 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19156 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
8 ms |
19180 KB |
Output is correct |
4 |
Correct |
8 ms |
19156 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
9 ms |
19200 KB |
Output is correct |
7 |
Correct |
8 ms |
19240 KB |
Output is correct |
8 |
Correct |
8 ms |
19248 KB |
Output is correct |
9 |
Correct |
8 ms |
19244 KB |
Output is correct |
10 |
Correct |
8 ms |
19176 KB |
Output is correct |
11 |
Correct |
8 ms |
19284 KB |
Output is correct |
12 |
Correct |
9 ms |
19312 KB |
Output is correct |
13 |
Correct |
9 ms |
19296 KB |
Output is correct |
14 |
Correct |
9 ms |
19252 KB |
Output is correct |
15 |
Correct |
8 ms |
19284 KB |
Output is correct |
16 |
Correct |
9 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19256 KB |
Output is correct |
18 |
Correct |
11 ms |
20376 KB |
Output is correct |
19 |
Correct |
11 ms |
20424 KB |
Output is correct |
20 |
Correct |
11 ms |
20436 KB |
Output is correct |
21 |
Correct |
11 ms |
20292 KB |
Output is correct |
22 |
Correct |
10 ms |
20420 KB |
Output is correct |
23 |
Correct |
11 ms |
20400 KB |
Output is correct |
24 |
Correct |
10 ms |
20352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19156 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
8 ms |
19180 KB |
Output is correct |
4 |
Correct |
8 ms |
19156 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
9 ms |
19200 KB |
Output is correct |
7 |
Correct |
8 ms |
19240 KB |
Output is correct |
8 |
Correct |
8 ms |
19248 KB |
Output is correct |
9 |
Correct |
8 ms |
19244 KB |
Output is correct |
10 |
Correct |
8 ms |
19176 KB |
Output is correct |
11 |
Correct |
8 ms |
19284 KB |
Output is correct |
12 |
Correct |
9 ms |
19312 KB |
Output is correct |
13 |
Correct |
9 ms |
19296 KB |
Output is correct |
14 |
Correct |
9 ms |
19252 KB |
Output is correct |
15 |
Correct |
8 ms |
19284 KB |
Output is correct |
16 |
Correct |
9 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19256 KB |
Output is correct |
18 |
Correct |
11 ms |
20376 KB |
Output is correct |
19 |
Correct |
11 ms |
20424 KB |
Output is correct |
20 |
Correct |
11 ms |
20436 KB |
Output is correct |
21 |
Correct |
11 ms |
20292 KB |
Output is correct |
22 |
Correct |
10 ms |
20420 KB |
Output is correct |
23 |
Correct |
11 ms |
20400 KB |
Output is correct |
24 |
Correct |
10 ms |
20352 KB |
Output is correct |
25 |
Correct |
8 ms |
19260 KB |
Output is correct |
26 |
Correct |
10 ms |
20416 KB |
Output is correct |
27 |
Correct |
11 ms |
20300 KB |
Output is correct |
28 |
Correct |
10 ms |
20416 KB |
Output is correct |
29 |
Correct |
11 ms |
20308 KB |
Output is correct |
30 |
Correct |
11 ms |
20204 KB |
Output is correct |
31 |
Correct |
10 ms |
20288 KB |
Output is correct |
32 |
Correct |
11 ms |
20180 KB |
Output is correct |
33 |
Correct |
10 ms |
20180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19156 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
8 ms |
19180 KB |
Output is correct |
4 |
Correct |
8 ms |
19156 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
9 ms |
19200 KB |
Output is correct |
7 |
Correct |
8 ms |
19240 KB |
Output is correct |
8 |
Correct |
8 ms |
19248 KB |
Output is correct |
9 |
Correct |
8 ms |
19244 KB |
Output is correct |
10 |
Correct |
8 ms |
19176 KB |
Output is correct |
11 |
Correct |
8 ms |
19284 KB |
Output is correct |
12 |
Correct |
9 ms |
19312 KB |
Output is correct |
13 |
Correct |
9 ms |
19296 KB |
Output is correct |
14 |
Correct |
9 ms |
19252 KB |
Output is correct |
15 |
Correct |
8 ms |
19284 KB |
Output is correct |
16 |
Correct |
9 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19256 KB |
Output is correct |
18 |
Correct |
11 ms |
20376 KB |
Output is correct |
19 |
Correct |
11 ms |
20424 KB |
Output is correct |
20 |
Correct |
11 ms |
20436 KB |
Output is correct |
21 |
Correct |
11 ms |
20292 KB |
Output is correct |
22 |
Correct |
10 ms |
20420 KB |
Output is correct |
23 |
Correct |
11 ms |
20400 KB |
Output is correct |
24 |
Correct |
10 ms |
20352 KB |
Output is correct |
25 |
Correct |
96 ms |
65364 KB |
Output is correct |
26 |
Correct |
84 ms |
69864 KB |
Output is correct |
27 |
Correct |
98 ms |
69896 KB |
Output is correct |
28 |
Correct |
117 ms |
69348 KB |
Output is correct |
29 |
Correct |
117 ms |
65260 KB |
Output is correct |
30 |
Correct |
125 ms |
68308 KB |
Output is correct |
31 |
Correct |
123 ms |
67360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19156 KB |
Output is correct |
2 |
Correct |
10 ms |
19256 KB |
Output is correct |
3 |
Correct |
170 ms |
61888 KB |
Output is correct |
4 |
Correct |
156 ms |
61908 KB |
Output is correct |
5 |
Correct |
174 ms |
62032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19156 KB |
Output is correct |
2 |
Correct |
9 ms |
19156 KB |
Output is correct |
3 |
Correct |
8 ms |
19180 KB |
Output is correct |
4 |
Correct |
8 ms |
19156 KB |
Output is correct |
5 |
Correct |
8 ms |
19204 KB |
Output is correct |
6 |
Correct |
9 ms |
19200 KB |
Output is correct |
7 |
Correct |
8 ms |
19240 KB |
Output is correct |
8 |
Correct |
8 ms |
19248 KB |
Output is correct |
9 |
Correct |
8 ms |
19244 KB |
Output is correct |
10 |
Correct |
8 ms |
19176 KB |
Output is correct |
11 |
Correct |
8 ms |
19284 KB |
Output is correct |
12 |
Correct |
9 ms |
19312 KB |
Output is correct |
13 |
Correct |
9 ms |
19296 KB |
Output is correct |
14 |
Correct |
9 ms |
19252 KB |
Output is correct |
15 |
Correct |
8 ms |
19284 KB |
Output is correct |
16 |
Correct |
9 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19256 KB |
Output is correct |
18 |
Correct |
11 ms |
20376 KB |
Output is correct |
19 |
Correct |
11 ms |
20424 KB |
Output is correct |
20 |
Correct |
11 ms |
20436 KB |
Output is correct |
21 |
Correct |
11 ms |
20292 KB |
Output is correct |
22 |
Correct |
10 ms |
20420 KB |
Output is correct |
23 |
Correct |
11 ms |
20400 KB |
Output is correct |
24 |
Correct |
10 ms |
20352 KB |
Output is correct |
25 |
Correct |
8 ms |
19260 KB |
Output is correct |
26 |
Correct |
10 ms |
20416 KB |
Output is correct |
27 |
Correct |
11 ms |
20300 KB |
Output is correct |
28 |
Correct |
10 ms |
20416 KB |
Output is correct |
29 |
Correct |
11 ms |
20308 KB |
Output is correct |
30 |
Correct |
11 ms |
20204 KB |
Output is correct |
31 |
Correct |
10 ms |
20288 KB |
Output is correct |
32 |
Correct |
11 ms |
20180 KB |
Output is correct |
33 |
Correct |
10 ms |
20180 KB |
Output is correct |
34 |
Correct |
96 ms |
65364 KB |
Output is correct |
35 |
Correct |
84 ms |
69864 KB |
Output is correct |
36 |
Correct |
98 ms |
69896 KB |
Output is correct |
37 |
Correct |
117 ms |
69348 KB |
Output is correct |
38 |
Correct |
117 ms |
65260 KB |
Output is correct |
39 |
Correct |
125 ms |
68308 KB |
Output is correct |
40 |
Correct |
123 ms |
67360 KB |
Output is correct |
41 |
Correct |
8 ms |
19156 KB |
Output is correct |
42 |
Correct |
10 ms |
19256 KB |
Output is correct |
43 |
Correct |
170 ms |
61888 KB |
Output is correct |
44 |
Correct |
156 ms |
61908 KB |
Output is correct |
45 |
Correct |
174 ms |
62032 KB |
Output is correct |
46 |
Correct |
104 ms |
67500 KB |
Output is correct |
47 |
Correct |
104 ms |
67600 KB |
Output is correct |
48 |
Correct |
118 ms |
67616 KB |
Output is correct |
49 |
Correct |
108 ms |
67528 KB |
Output is correct |
50 |
Correct |
196 ms |
61596 KB |
Output is correct |
51 |
Correct |
195 ms |
61500 KB |
Output is correct |
52 |
Correct |
237 ms |
61512 KB |
Output is correct |
53 |
Correct |
184 ms |
61492 KB |
Output is correct |