#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
/* cat exercises:
*/
const int N=2e5+5000,K=30;
int n,h[N],f[N][K],dep[N],dp[N];
vector<int> g[N];
void setf(int u,int fa){
f[u][0]=fa;
for(int i=1;i<K;i++)
f[u][i]=f[f[u][i-1]][i-1];
for(auto&v:g[u]) if(v!=fa) setf(v,u);
return;
}
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
for(auto&v:g[u]) if(v!=fa) dfs(v,u);
}
int lca(int x,int y){
if (dep[x] < dep[y]) swap(x, y);
for(int i=27;i>=0;i--) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for(int i=27;i>=0;i--) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
vector<int> par;
void init(int sz){
par.resize(sz);
for(int i=0;i<sz;i++) par[i]=i;
}
int find(int x){
if(par[x]==x) return x;
else return par[x]=find(par[x]);
}
int dis(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)];}
void solve(){
cin>>n;
init(n+500);
for(int i=1;i<=n;i++) cin>>h[i];
for(int i=0;i<n-1;i++){
int u,v; cin>>u>>v;
u=h[u]; v=h[v];
g[u].pb(v); g[v].pb(u);
}
setf(1,0);
dfs(1,0);
for(int u=1;u<=n;u++){
for(auto&v:g[u]){
v=find(v);
if(v<u){
par[v]=u;
dp[u]=max(dp[u],dp[v]+dis(u,v));
}
}
}
cout<<dp[n]<<'\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t tStart = clock();
int local=0,multi=0,debug=1,tt=1;
if(local){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
if(multi) cin>>tt;
for(int i=1;i<=tt;i++){
if(debug && multi && local) cout<<"样例 "<<i<<'\n';
solve();
}
fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:76:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:77:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5184 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
2 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5184 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
2 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6648 KB |
Output is correct |
19 |
Correct |
6 ms |
6868 KB |
Output is correct |
20 |
Correct |
6 ms |
6868 KB |
Output is correct |
21 |
Correct |
6 ms |
6740 KB |
Output is correct |
22 |
Correct |
6 ms |
6868 KB |
Output is correct |
23 |
Correct |
5 ms |
6740 KB |
Output is correct |
24 |
Correct |
5 ms |
6868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5184 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
2 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6648 KB |
Output is correct |
19 |
Correct |
6 ms |
6868 KB |
Output is correct |
20 |
Correct |
6 ms |
6868 KB |
Output is correct |
21 |
Correct |
6 ms |
6740 KB |
Output is correct |
22 |
Correct |
6 ms |
6868 KB |
Output is correct |
23 |
Correct |
5 ms |
6740 KB |
Output is correct |
24 |
Correct |
5 ms |
6868 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
8 ms |
6740 KB |
Output is correct |
27 |
Correct |
5 ms |
6740 KB |
Output is correct |
28 |
Correct |
5 ms |
6740 KB |
Output is correct |
29 |
Correct |
6 ms |
6800 KB |
Output is correct |
30 |
Correct |
6 ms |
6612 KB |
Output is correct |
31 |
Correct |
6 ms |
6612 KB |
Output is correct |
32 |
Correct |
6 ms |
6612 KB |
Output is correct |
33 |
Correct |
6 ms |
6588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5184 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
2 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6648 KB |
Output is correct |
19 |
Correct |
6 ms |
6868 KB |
Output is correct |
20 |
Correct |
6 ms |
6868 KB |
Output is correct |
21 |
Correct |
6 ms |
6740 KB |
Output is correct |
22 |
Correct |
6 ms |
6868 KB |
Output is correct |
23 |
Correct |
5 ms |
6740 KB |
Output is correct |
24 |
Correct |
5 ms |
6868 KB |
Output is correct |
25 |
Correct |
254 ms |
69284 KB |
Output is correct |
26 |
Correct |
193 ms |
73852 KB |
Output is correct |
27 |
Correct |
200 ms |
73884 KB |
Output is correct |
28 |
Correct |
353 ms |
73440 KB |
Output is correct |
29 |
Correct |
329 ms |
69432 KB |
Output is correct |
30 |
Correct |
334 ms |
72308 KB |
Output is correct |
31 |
Correct |
337 ms |
71508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5204 KB |
Output is correct |
3 |
Correct |
269 ms |
66204 KB |
Output is correct |
4 |
Correct |
274 ms |
66148 KB |
Output is correct |
5 |
Correct |
253 ms |
66108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5204 KB |
Output is correct |
12 |
Correct |
2 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5184 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5204 KB |
Output is correct |
16 |
Correct |
2 ms |
5204 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6648 KB |
Output is correct |
19 |
Correct |
6 ms |
6868 KB |
Output is correct |
20 |
Correct |
6 ms |
6868 KB |
Output is correct |
21 |
Correct |
6 ms |
6740 KB |
Output is correct |
22 |
Correct |
6 ms |
6868 KB |
Output is correct |
23 |
Correct |
5 ms |
6740 KB |
Output is correct |
24 |
Correct |
5 ms |
6868 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
8 ms |
6740 KB |
Output is correct |
27 |
Correct |
5 ms |
6740 KB |
Output is correct |
28 |
Correct |
5 ms |
6740 KB |
Output is correct |
29 |
Correct |
6 ms |
6800 KB |
Output is correct |
30 |
Correct |
6 ms |
6612 KB |
Output is correct |
31 |
Correct |
6 ms |
6612 KB |
Output is correct |
32 |
Correct |
6 ms |
6612 KB |
Output is correct |
33 |
Correct |
6 ms |
6588 KB |
Output is correct |
34 |
Correct |
254 ms |
69284 KB |
Output is correct |
35 |
Correct |
193 ms |
73852 KB |
Output is correct |
36 |
Correct |
200 ms |
73884 KB |
Output is correct |
37 |
Correct |
353 ms |
73440 KB |
Output is correct |
38 |
Correct |
329 ms |
69432 KB |
Output is correct |
39 |
Correct |
334 ms |
72308 KB |
Output is correct |
40 |
Correct |
337 ms |
71508 KB |
Output is correct |
41 |
Correct |
2 ms |
5076 KB |
Output is correct |
42 |
Correct |
2 ms |
5204 KB |
Output is correct |
43 |
Correct |
269 ms |
66204 KB |
Output is correct |
44 |
Correct |
274 ms |
66148 KB |
Output is correct |
45 |
Correct |
253 ms |
66108 KB |
Output is correct |
46 |
Correct |
273 ms |
71692 KB |
Output is correct |
47 |
Correct |
259 ms |
71656 KB |
Output is correct |
48 |
Correct |
292 ms |
71592 KB |
Output is correct |
49 |
Correct |
297 ms |
71632 KB |
Output is correct |
50 |
Correct |
304 ms |
65612 KB |
Output is correct |
51 |
Correct |
315 ms |
65592 KB |
Output is correct |
52 |
Correct |
310 ms |
65660 KB |
Output is correct |
53 |
Correct |
320 ms |
65668 KB |
Output is correct |