#include <bits/stdc++.h>
using namespace std;
#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];
return;
}
void dfs(int u,int fa){
setf(u,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 n){
par.resize(n);
for(int i=0;i<n;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);
}
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:73:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:74:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5200 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 |
3 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 |
3 ms |
5200 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 |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 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 |
2 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5200 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 |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 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 |
2 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
5 ms |
6100 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
6100 KB |
Output is correct |
24 |
Correct |
5 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5200 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 |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 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 |
2 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
5 ms |
6100 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
6100 KB |
Output is correct |
24 |
Correct |
5 ms |
6100 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
6100 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
5 ms |
6100 KB |
Output is correct |
29 |
Correct |
5 ms |
6100 KB |
Output is correct |
30 |
Correct |
5 ms |
5972 KB |
Output is correct |
31 |
Correct |
6 ms |
5972 KB |
Output is correct |
32 |
Correct |
6 ms |
6100 KB |
Output is correct |
33 |
Correct |
5 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5200 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 |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 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 |
2 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
5 ms |
6100 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
6100 KB |
Output is correct |
24 |
Correct |
5 ms |
6100 KB |
Output is correct |
25 |
Incorrect |
151 ms |
42700 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
205 ms |
37932 KB |
Output is correct |
4 |
Correct |
214 ms |
37964 KB |
Output is correct |
5 |
Correct |
206 ms |
38004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5200 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 |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 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 |
2 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
5 ms |
6100 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
6100 KB |
Output is correct |
24 |
Correct |
5 ms |
6100 KB |
Output is correct |
25 |
Correct |
2 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
6100 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
5 ms |
6100 KB |
Output is correct |
29 |
Correct |
5 ms |
6100 KB |
Output is correct |
30 |
Correct |
5 ms |
5972 KB |
Output is correct |
31 |
Correct |
6 ms |
5972 KB |
Output is correct |
32 |
Correct |
6 ms |
6100 KB |
Output is correct |
33 |
Correct |
5 ms |
5972 KB |
Output is correct |
34 |
Incorrect |
151 ms |
42700 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |