/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
ll dp[N], dep[N];
int _lca(int u, int v);
struct Dsu{
vector<int> s, p, mx;
Dsu(int n){
s.resize(n+1, 1);
p.resize(n+1);
mx.resize(n+1);
for(int i = 1; i <= n; ++i) p[i] = i, mx[i] = i;
}
int find(int v){
if(p[v] == v) return v;
return p[v] = find(p[v]);
}
void merge(int a, int b){
int u = find(a);
int v = find(b); // b is the bigger vertex
if(u != v){
// cout << a << ' ' << b << ' ' <<mx[u] << '\n';
dp[b] = max(dp[b], dp[mx[u]] + dep[mx[u]] + dep[b] - 2 * dep[_lca(mx[u], b)]);
if(s[u] < s[v]) swap(u, v);
mx[u] = max(mx[v], mx[u]);
p[v] = u;
s[u] += s[v];
}
}
};
int n, a[N], timer=0, tin[N], tout[N], up[N][K];
vector<array<int, 2>> edges;
vector<int> g[N];
void dfs(int v, int p){
dep[v] = dep[p] + 1;
up[v][0] = p;
tin[v] = timer++;
for(int u: g[v]) if(u != p) dfs(u, v);
tout[v] = timer++;
}
bool is(int u, int v){
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int _lca(int u, int v){
if(is(u, v)) return u;
if(is(v, u)) return v;
for(int j = K-1; j >= 0; --j) if(!is(up[u][j], v)) u = up[u][j];
return up[u][0];
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 0; i < n-1; ++i){
int u, v; cin >> u >> v;
u = a[u], v = a[v];
g[u].pb(v);
g[v].pb(u);
edges.pb({max(u, v), min(u, v)});
}
sort(all(edges));
dep[1] = 0;
dfs(1, 1);
for(int j = 1; j < K; ++j) for(int i = 1; i <= n; ++i) up[i][j] = up[up[i][j - 1]][j - 1];
Dsu d(n);
for(auto e: edges){
int u = e[0], v = e[1];
// cout << u << ' ' << v << '\n';
d.merge(v, u);
}
// for(int i = 1; i <= n; ++i) cout << dp[i] << ' ';
cout << dp[n];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:96:15: warning: unused variable 'aa' [-Wunused-variable]
96 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
33372 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33368 KB |
Output is correct |
5 |
Correct |
8 ms |
33372 KB |
Output is correct |
6 |
Correct |
8 ms |
33368 KB |
Output is correct |
7 |
Correct |
8 ms |
33372 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
33372 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33368 KB |
Output is correct |
5 |
Correct |
8 ms |
33372 KB |
Output is correct |
6 |
Correct |
8 ms |
33368 KB |
Output is correct |
7 |
Correct |
8 ms |
33372 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
11 |
Correct |
9 ms |
33624 KB |
Output is correct |
12 |
Correct |
8 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33448 KB |
Output is correct |
14 |
Correct |
8 ms |
33372 KB |
Output is correct |
15 |
Correct |
8 ms |
33372 KB |
Output is correct |
16 |
Correct |
8 ms |
33372 KB |
Output is correct |
17 |
Correct |
8 ms |
33372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
33372 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33368 KB |
Output is correct |
5 |
Correct |
8 ms |
33372 KB |
Output is correct |
6 |
Correct |
8 ms |
33368 KB |
Output is correct |
7 |
Correct |
8 ms |
33372 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
11 |
Correct |
9 ms |
33624 KB |
Output is correct |
12 |
Correct |
8 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33448 KB |
Output is correct |
14 |
Correct |
8 ms |
33372 KB |
Output is correct |
15 |
Correct |
8 ms |
33372 KB |
Output is correct |
16 |
Correct |
8 ms |
33372 KB |
Output is correct |
17 |
Correct |
8 ms |
33372 KB |
Output is correct |
18 |
Correct |
12 ms |
33884 KB |
Output is correct |
19 |
Correct |
11 ms |
33928 KB |
Output is correct |
20 |
Correct |
11 ms |
33884 KB |
Output is correct |
21 |
Correct |
11 ms |
33988 KB |
Output is correct |
22 |
Correct |
12 ms |
33884 KB |
Output is correct |
23 |
Correct |
12 ms |
33884 KB |
Output is correct |
24 |
Correct |
11 ms |
33884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
33372 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33368 KB |
Output is correct |
5 |
Correct |
8 ms |
33372 KB |
Output is correct |
6 |
Correct |
8 ms |
33368 KB |
Output is correct |
7 |
Correct |
8 ms |
33372 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
11 |
Correct |
9 ms |
33624 KB |
Output is correct |
12 |
Correct |
8 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33448 KB |
Output is correct |
14 |
Correct |
8 ms |
33372 KB |
Output is correct |
15 |
Correct |
8 ms |
33372 KB |
Output is correct |
16 |
Correct |
8 ms |
33372 KB |
Output is correct |
17 |
Correct |
8 ms |
33372 KB |
Output is correct |
18 |
Correct |
12 ms |
33884 KB |
Output is correct |
19 |
Correct |
11 ms |
33928 KB |
Output is correct |
20 |
Correct |
11 ms |
33884 KB |
Output is correct |
21 |
Correct |
11 ms |
33988 KB |
Output is correct |
22 |
Correct |
12 ms |
33884 KB |
Output is correct |
23 |
Correct |
12 ms |
33884 KB |
Output is correct |
24 |
Correct |
11 ms |
33884 KB |
Output is correct |
25 |
Correct |
8 ms |
33368 KB |
Output is correct |
26 |
Correct |
12 ms |
33884 KB |
Output is correct |
27 |
Correct |
13 ms |
34020 KB |
Output is correct |
28 |
Correct |
11 ms |
33880 KB |
Output is correct |
29 |
Correct |
11 ms |
34016 KB |
Output is correct |
30 |
Correct |
12 ms |
33740 KB |
Output is correct |
31 |
Correct |
13 ms |
33660 KB |
Output is correct |
32 |
Correct |
11 ms |
33628 KB |
Output is correct |
33 |
Correct |
11 ms |
33628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
33372 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33368 KB |
Output is correct |
5 |
Correct |
8 ms |
33372 KB |
Output is correct |
6 |
Correct |
8 ms |
33368 KB |
Output is correct |
7 |
Correct |
8 ms |
33372 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
11 |
Correct |
9 ms |
33624 KB |
Output is correct |
12 |
Correct |
8 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33448 KB |
Output is correct |
14 |
Correct |
8 ms |
33372 KB |
Output is correct |
15 |
Correct |
8 ms |
33372 KB |
Output is correct |
16 |
Correct |
8 ms |
33372 KB |
Output is correct |
17 |
Correct |
8 ms |
33372 KB |
Output is correct |
18 |
Correct |
12 ms |
33884 KB |
Output is correct |
19 |
Correct |
11 ms |
33928 KB |
Output is correct |
20 |
Correct |
11 ms |
33884 KB |
Output is correct |
21 |
Correct |
11 ms |
33988 KB |
Output is correct |
22 |
Correct |
12 ms |
33884 KB |
Output is correct |
23 |
Correct |
12 ms |
33884 KB |
Output is correct |
24 |
Correct |
11 ms |
33884 KB |
Output is correct |
25 |
Correct |
241 ms |
96704 KB |
Output is correct |
26 |
Correct |
161 ms |
101132 KB |
Output is correct |
27 |
Correct |
162 ms |
101308 KB |
Output is correct |
28 |
Correct |
226 ms |
100896 KB |
Output is correct |
29 |
Correct |
194 ms |
96800 KB |
Output is correct |
30 |
Correct |
175 ms |
99772 KB |
Output is correct |
31 |
Correct |
180 ms |
98752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
33372 KB |
Output is correct |
2 |
Correct |
9 ms |
33368 KB |
Output is correct |
3 |
Correct |
214 ms |
91800 KB |
Output is correct |
4 |
Correct |
205 ms |
92092 KB |
Output is correct |
5 |
Correct |
212 ms |
91836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
33372 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
9 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33368 KB |
Output is correct |
5 |
Correct |
8 ms |
33372 KB |
Output is correct |
6 |
Correct |
8 ms |
33368 KB |
Output is correct |
7 |
Correct |
8 ms |
33372 KB |
Output is correct |
8 |
Correct |
8 ms |
33368 KB |
Output is correct |
9 |
Correct |
8 ms |
33372 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
11 |
Correct |
9 ms |
33624 KB |
Output is correct |
12 |
Correct |
8 ms |
33372 KB |
Output is correct |
13 |
Correct |
8 ms |
33448 KB |
Output is correct |
14 |
Correct |
8 ms |
33372 KB |
Output is correct |
15 |
Correct |
8 ms |
33372 KB |
Output is correct |
16 |
Correct |
8 ms |
33372 KB |
Output is correct |
17 |
Correct |
8 ms |
33372 KB |
Output is correct |
18 |
Correct |
12 ms |
33884 KB |
Output is correct |
19 |
Correct |
11 ms |
33928 KB |
Output is correct |
20 |
Correct |
11 ms |
33884 KB |
Output is correct |
21 |
Correct |
11 ms |
33988 KB |
Output is correct |
22 |
Correct |
12 ms |
33884 KB |
Output is correct |
23 |
Correct |
12 ms |
33884 KB |
Output is correct |
24 |
Correct |
11 ms |
33884 KB |
Output is correct |
25 |
Correct |
8 ms |
33368 KB |
Output is correct |
26 |
Correct |
12 ms |
33884 KB |
Output is correct |
27 |
Correct |
13 ms |
34020 KB |
Output is correct |
28 |
Correct |
11 ms |
33880 KB |
Output is correct |
29 |
Correct |
11 ms |
34016 KB |
Output is correct |
30 |
Correct |
12 ms |
33740 KB |
Output is correct |
31 |
Correct |
13 ms |
33660 KB |
Output is correct |
32 |
Correct |
11 ms |
33628 KB |
Output is correct |
33 |
Correct |
11 ms |
33628 KB |
Output is correct |
34 |
Correct |
241 ms |
96704 KB |
Output is correct |
35 |
Correct |
161 ms |
101132 KB |
Output is correct |
36 |
Correct |
162 ms |
101308 KB |
Output is correct |
37 |
Correct |
226 ms |
100896 KB |
Output is correct |
38 |
Correct |
194 ms |
96800 KB |
Output is correct |
39 |
Correct |
175 ms |
99772 KB |
Output is correct |
40 |
Correct |
180 ms |
98752 KB |
Output is correct |
41 |
Correct |
8 ms |
33372 KB |
Output is correct |
42 |
Correct |
9 ms |
33368 KB |
Output is correct |
43 |
Correct |
214 ms |
91800 KB |
Output is correct |
44 |
Correct |
205 ms |
92092 KB |
Output is correct |
45 |
Correct |
212 ms |
91836 KB |
Output is correct |
46 |
Correct |
181 ms |
98496 KB |
Output is correct |
47 |
Correct |
168 ms |
98824 KB |
Output is correct |
48 |
Correct |
174 ms |
98496 KB |
Output is correct |
49 |
Correct |
170 ms |
98692 KB |
Output is correct |
50 |
Correct |
255 ms |
92476 KB |
Output is correct |
51 |
Correct |
227 ms |
92048 KB |
Output is correct |
52 |
Correct |
224 ms |
92096 KB |
Output is correct |
53 |
Correct |
239 ms |
92088 KB |
Output is correct |