#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=22;
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=20;i>=0;i--) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for(int i=20;i>=0;i--) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
struct dsu{
vector<int> par,sz;
int total_groups;
void init(int n){
par.resize(n); sz.resize(n);
for(int i=0;i<n;i++){
par[i]=i;
sz[i]=1;
}
}
int find(int x){
if(par[x]==x) return x;
else return par[x]=find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
par[y] = x;
sz[x] += sz[y];
total_groups--;
}
bool same(int x,int y){
x = find(x);
y = find(y);
if(x==y) return true;
else return false;
}
};
//dsu ds; ds.init(n+5); ds.find(x); ds.unite(x,y); ds.same(x,y);
int dis(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)];}
void solve(){
cin>>n;
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);
}
dsu ds; ds.init(N+500);
dfs(1,0);
for(int u=1;u<=n;u++){
for(auto&v:g[u]){
v=ds.find(v);
if(v<u){
ds.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:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:96:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
3 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
3 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
3 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
3 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
6780 KB |
Output is correct |
12 |
Correct |
3 ms |
6740 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
3 ms |
6740 KB |
Output is correct |
15 |
Correct |
3 ms |
6740 KB |
Output is correct |
16 |
Correct |
3 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
3 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
3 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
6780 KB |
Output is correct |
12 |
Correct |
3 ms |
6740 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
3 ms |
6740 KB |
Output is correct |
15 |
Correct |
3 ms |
6740 KB |
Output is correct |
16 |
Correct |
3 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
19 |
Correct |
6 ms |
7636 KB |
Output is correct |
20 |
Correct |
6 ms |
7636 KB |
Output is correct |
21 |
Correct |
6 ms |
7508 KB |
Output is correct |
22 |
Correct |
6 ms |
7636 KB |
Output is correct |
23 |
Correct |
8 ms |
7508 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
3 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
3 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
6780 KB |
Output is correct |
12 |
Correct |
3 ms |
6740 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
3 ms |
6740 KB |
Output is correct |
15 |
Correct |
3 ms |
6740 KB |
Output is correct |
16 |
Correct |
3 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
19 |
Correct |
6 ms |
7636 KB |
Output is correct |
20 |
Correct |
6 ms |
7636 KB |
Output is correct |
21 |
Correct |
6 ms |
7508 KB |
Output is correct |
22 |
Correct |
6 ms |
7636 KB |
Output is correct |
23 |
Correct |
8 ms |
7508 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
25 |
Correct |
3 ms |
6740 KB |
Output is correct |
26 |
Correct |
5 ms |
7508 KB |
Output is correct |
27 |
Correct |
6 ms |
7508 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
6 ms |
7348 KB |
Output is correct |
31 |
Correct |
6 ms |
7380 KB |
Output is correct |
32 |
Correct |
6 ms |
7380 KB |
Output is correct |
33 |
Correct |
6 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
3 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
3 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
6780 KB |
Output is correct |
12 |
Correct |
3 ms |
6740 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
3 ms |
6740 KB |
Output is correct |
15 |
Correct |
3 ms |
6740 KB |
Output is correct |
16 |
Correct |
3 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
19 |
Correct |
6 ms |
7636 KB |
Output is correct |
20 |
Correct |
6 ms |
7636 KB |
Output is correct |
21 |
Correct |
6 ms |
7508 KB |
Output is correct |
22 |
Correct |
6 ms |
7636 KB |
Output is correct |
23 |
Correct |
8 ms |
7508 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
25 |
Incorrect |
135 ms |
37172 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
198 ms |
32532 KB |
Output is correct |
4 |
Correct |
179 ms |
32508 KB |
Output is correct |
5 |
Correct |
178 ms |
32580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6740 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
3 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
3 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
3 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
6780 KB |
Output is correct |
12 |
Correct |
3 ms |
6740 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
3 ms |
6740 KB |
Output is correct |
15 |
Correct |
3 ms |
6740 KB |
Output is correct |
16 |
Correct |
3 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
6 ms |
7508 KB |
Output is correct |
19 |
Correct |
6 ms |
7636 KB |
Output is correct |
20 |
Correct |
6 ms |
7636 KB |
Output is correct |
21 |
Correct |
6 ms |
7508 KB |
Output is correct |
22 |
Correct |
6 ms |
7636 KB |
Output is correct |
23 |
Correct |
8 ms |
7508 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
25 |
Correct |
3 ms |
6740 KB |
Output is correct |
26 |
Correct |
5 ms |
7508 KB |
Output is correct |
27 |
Correct |
6 ms |
7508 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
6 ms |
7348 KB |
Output is correct |
31 |
Correct |
6 ms |
7380 KB |
Output is correct |
32 |
Correct |
6 ms |
7380 KB |
Output is correct |
33 |
Correct |
6 ms |
7380 KB |
Output is correct |
34 |
Incorrect |
135 ms |
37172 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |