#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=1e5+5000,K=25;
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+5);
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<<*max_element(dp+1,dp+1+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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2784 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
1 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2772 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2784 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
1 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2772 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2784 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
1 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2772 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
3668 KB |
Output is correct |
19 |
Correct |
4 ms |
3668 KB |
Output is correct |
20 |
Correct |
4 ms |
3668 KB |
Output is correct |
21 |
Correct |
4 ms |
3668 KB |
Output is correct |
22 |
Correct |
4 ms |
3668 KB |
Output is correct |
23 |
Correct |
5 ms |
3668 KB |
Output is correct |
24 |
Correct |
4 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2784 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
1 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2772 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
3668 KB |
Output is correct |
19 |
Correct |
4 ms |
3668 KB |
Output is correct |
20 |
Correct |
4 ms |
3668 KB |
Output is correct |
21 |
Correct |
4 ms |
3668 KB |
Output is correct |
22 |
Correct |
4 ms |
3668 KB |
Output is correct |
23 |
Correct |
5 ms |
3668 KB |
Output is correct |
24 |
Correct |
4 ms |
3668 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
4 ms |
3668 KB |
Output is correct |
27 |
Correct |
4 ms |
3668 KB |
Output is correct |
28 |
Correct |
4 ms |
3668 KB |
Output is correct |
29 |
Correct |
4 ms |
3668 KB |
Output is correct |
30 |
Correct |
4 ms |
3540 KB |
Output is correct |
31 |
Correct |
4 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3540 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2784 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
1 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2772 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
3668 KB |
Output is correct |
19 |
Correct |
4 ms |
3668 KB |
Output is correct |
20 |
Correct |
4 ms |
3668 KB |
Output is correct |
21 |
Correct |
4 ms |
3668 KB |
Output is correct |
22 |
Correct |
4 ms |
3668 KB |
Output is correct |
23 |
Correct |
5 ms |
3668 KB |
Output is correct |
24 |
Correct |
4 ms |
3668 KB |
Output is correct |
25 |
Runtime error |
15 ms |
11220 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Runtime error |
11 ms |
6356 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2784 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
1 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
1 ms |
2772 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
3668 KB |
Output is correct |
19 |
Correct |
4 ms |
3668 KB |
Output is correct |
20 |
Correct |
4 ms |
3668 KB |
Output is correct |
21 |
Correct |
4 ms |
3668 KB |
Output is correct |
22 |
Correct |
4 ms |
3668 KB |
Output is correct |
23 |
Correct |
5 ms |
3668 KB |
Output is correct |
24 |
Correct |
4 ms |
3668 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
4 ms |
3668 KB |
Output is correct |
27 |
Correct |
4 ms |
3668 KB |
Output is correct |
28 |
Correct |
4 ms |
3668 KB |
Output is correct |
29 |
Correct |
4 ms |
3668 KB |
Output is correct |
30 |
Correct |
4 ms |
3540 KB |
Output is correct |
31 |
Correct |
4 ms |
3540 KB |
Output is correct |
32 |
Correct |
4 ms |
3540 KB |
Output is correct |
33 |
Correct |
4 ms |
3540 KB |
Output is correct |
34 |
Runtime error |
15 ms |
11220 KB |
Execution killed with signal 11 |
35 |
Halted |
0 ms |
0 KB |
- |