#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define imp(v) for(auto messi:v)cout<<messi<<" "; cout<<"\n"
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll MAXN=2e5+5;
ll uf[MAXN],pa[MAXN];
ll uf_find(ll x){return uf[x]<0?x:uf[x]=uf_find(uf[x]);}
void uf_join(ll x, ll y){
x=uf_find(x),y=uf_find(y);
if(-uf[x]>-uf[y])swap(x,y);
uf[x]+=uf[y];
uf[y]=x;
}
ll dp[MAXN];
vector<ll>g[MAXN]; ll n;
const ll K=18; //K such that 2^K>=n
ll F[K][MAXN],D[MAXN];
void lca_dfs(ll x){
for(auto y:g[x]){
if(y==F[0][x])continue;
F[0][y]=x;D[y]=D[x]+1;lca_dfs(y);
}
}
void lca_init(){
D[0]=0;F[0][0]=-1;
lca_dfs(0);
fore(k,1,K)fore(x,0,n){
if(F[k-1][x]<0)F[k][x]=-1;
else F[k][x]=F[k-1][F[k-1][x]];
}
}
ll lca(ll x,ll y){
if(D[x]<D[y])swap(x,y);
for(ll k=K-1;k>=0;k--)if(D[x]-(1ll<<k)>=D[y])x=F[k][x];
if(x==y)return x;
for(ll k=K-1;k>=0;k--)if(F[k][x]!=F[k][y])x=F[k][x],y=F[k][y];
return F[0][x];
}
ll dis(ll x, ll y){return D[x]+D[y]-2*D[lca(x,y)];}
int main(){FIN;
cin>>n;
vector<ll>a(n),p(n);
fore(i,0,n)cin>>a[i],a[i]--,p[a[i]]=i;
fore(i,0,n-1){
ll u,v; cin>>u>>v; u--,v--;
g[u].pb(v);
g[v].pb(u);
}
mset(uf,-1);
lca_init();
fore(i,0,n){
auto x=p[i];
for(auto y:g[x])if(a[y]<a[x]){
y=pa[uf_find(y)];
dp[x]=max(dp[x],dp[y]+dis(x,y));
uf_join(x,y);
}
pa[uf_find(x)]=x;
}
cout<<dp[p[n-1]]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
38748 KB |
Output is correct |
2 |
Correct |
5 ms |
38748 KB |
Output is correct |
3 |
Correct |
5 ms |
38748 KB |
Output is correct |
4 |
Correct |
6 ms |
38904 KB |
Output is correct |
5 |
Correct |
5 ms |
38748 KB |
Output is correct |
6 |
Correct |
5 ms |
38748 KB |
Output is correct |
7 |
Correct |
5 ms |
38744 KB |
Output is correct |
8 |
Correct |
5 ms |
38748 KB |
Output is correct |
9 |
Correct |
5 ms |
38748 KB |
Output is correct |
10 |
Correct |
5 ms |
38748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
38748 KB |
Output is correct |
2 |
Correct |
5 ms |
38748 KB |
Output is correct |
3 |
Correct |
5 ms |
38748 KB |
Output is correct |
4 |
Correct |
6 ms |
38904 KB |
Output is correct |
5 |
Correct |
5 ms |
38748 KB |
Output is correct |
6 |
Correct |
5 ms |
38748 KB |
Output is correct |
7 |
Correct |
5 ms |
38744 KB |
Output is correct |
8 |
Correct |
5 ms |
38748 KB |
Output is correct |
9 |
Correct |
5 ms |
38748 KB |
Output is correct |
10 |
Correct |
5 ms |
38748 KB |
Output is correct |
11 |
Correct |
5 ms |
38748 KB |
Output is correct |
12 |
Correct |
5 ms |
38836 KB |
Output is correct |
13 |
Correct |
5 ms |
38748 KB |
Output is correct |
14 |
Correct |
6 ms |
38840 KB |
Output is correct |
15 |
Correct |
5 ms |
38748 KB |
Output is correct |
16 |
Correct |
5 ms |
38748 KB |
Output is correct |
17 |
Correct |
5 ms |
38748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
38748 KB |
Output is correct |
2 |
Correct |
5 ms |
38748 KB |
Output is correct |
3 |
Correct |
5 ms |
38748 KB |
Output is correct |
4 |
Correct |
6 ms |
38904 KB |
Output is correct |
5 |
Correct |
5 ms |
38748 KB |
Output is correct |
6 |
Correct |
5 ms |
38748 KB |
Output is correct |
7 |
Correct |
5 ms |
38744 KB |
Output is correct |
8 |
Correct |
5 ms |
38748 KB |
Output is correct |
9 |
Correct |
5 ms |
38748 KB |
Output is correct |
10 |
Correct |
5 ms |
38748 KB |
Output is correct |
11 |
Correct |
5 ms |
38748 KB |
Output is correct |
12 |
Correct |
5 ms |
38836 KB |
Output is correct |
13 |
Correct |
5 ms |
38748 KB |
Output is correct |
14 |
Correct |
6 ms |
38840 KB |
Output is correct |
15 |
Correct |
5 ms |
38748 KB |
Output is correct |
16 |
Correct |
5 ms |
38748 KB |
Output is correct |
17 |
Correct |
5 ms |
38748 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39472 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39260 KB |
Output is correct |
22 |
Correct |
8 ms |
39460 KB |
Output is correct |
23 |
Correct |
7 ms |
39256 KB |
Output is correct |
24 |
Correct |
8 ms |
39260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
38748 KB |
Output is correct |
2 |
Correct |
5 ms |
38748 KB |
Output is correct |
3 |
Correct |
5 ms |
38748 KB |
Output is correct |
4 |
Correct |
6 ms |
38904 KB |
Output is correct |
5 |
Correct |
5 ms |
38748 KB |
Output is correct |
6 |
Correct |
5 ms |
38748 KB |
Output is correct |
7 |
Correct |
5 ms |
38744 KB |
Output is correct |
8 |
Correct |
5 ms |
38748 KB |
Output is correct |
9 |
Correct |
5 ms |
38748 KB |
Output is correct |
10 |
Correct |
5 ms |
38748 KB |
Output is correct |
11 |
Correct |
5 ms |
38748 KB |
Output is correct |
12 |
Correct |
5 ms |
38836 KB |
Output is correct |
13 |
Correct |
5 ms |
38748 KB |
Output is correct |
14 |
Correct |
6 ms |
38840 KB |
Output is correct |
15 |
Correct |
5 ms |
38748 KB |
Output is correct |
16 |
Correct |
5 ms |
38748 KB |
Output is correct |
17 |
Correct |
5 ms |
38748 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39472 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39260 KB |
Output is correct |
22 |
Correct |
8 ms |
39460 KB |
Output is correct |
23 |
Correct |
7 ms |
39256 KB |
Output is correct |
24 |
Correct |
8 ms |
39260 KB |
Output is correct |
25 |
Correct |
6 ms |
38948 KB |
Output is correct |
26 |
Correct |
8 ms |
39260 KB |
Output is correct |
27 |
Correct |
9 ms |
39472 KB |
Output is correct |
28 |
Correct |
8 ms |
39260 KB |
Output is correct |
29 |
Correct |
7 ms |
39260 KB |
Output is correct |
30 |
Correct |
8 ms |
39256 KB |
Output is correct |
31 |
Correct |
8 ms |
39260 KB |
Output is correct |
32 |
Correct |
9 ms |
39140 KB |
Output is correct |
33 |
Correct |
8 ms |
39260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
38748 KB |
Output is correct |
2 |
Correct |
5 ms |
38748 KB |
Output is correct |
3 |
Correct |
5 ms |
38748 KB |
Output is correct |
4 |
Correct |
6 ms |
38904 KB |
Output is correct |
5 |
Correct |
5 ms |
38748 KB |
Output is correct |
6 |
Correct |
5 ms |
38748 KB |
Output is correct |
7 |
Correct |
5 ms |
38744 KB |
Output is correct |
8 |
Correct |
5 ms |
38748 KB |
Output is correct |
9 |
Correct |
5 ms |
38748 KB |
Output is correct |
10 |
Correct |
5 ms |
38748 KB |
Output is correct |
11 |
Correct |
5 ms |
38748 KB |
Output is correct |
12 |
Correct |
5 ms |
38836 KB |
Output is correct |
13 |
Correct |
5 ms |
38748 KB |
Output is correct |
14 |
Correct |
6 ms |
38840 KB |
Output is correct |
15 |
Correct |
5 ms |
38748 KB |
Output is correct |
16 |
Correct |
5 ms |
38748 KB |
Output is correct |
17 |
Correct |
5 ms |
38748 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39472 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39260 KB |
Output is correct |
22 |
Correct |
8 ms |
39460 KB |
Output is correct |
23 |
Correct |
7 ms |
39256 KB |
Output is correct |
24 |
Correct |
8 ms |
39260 KB |
Output is correct |
25 |
Correct |
81 ms |
59988 KB |
Output is correct |
26 |
Correct |
87 ms |
59924 KB |
Output is correct |
27 |
Correct |
81 ms |
59932 KB |
Output is correct |
28 |
Correct |
116 ms |
60056 KB |
Output is correct |
29 |
Correct |
132 ms |
59948 KB |
Output is correct |
30 |
Correct |
116 ms |
60024 KB |
Output is correct |
31 |
Correct |
117 ms |
60048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
38744 KB |
Output is correct |
2 |
Correct |
6 ms |
38748 KB |
Output is correct |
3 |
Correct |
248 ms |
53620 KB |
Output is correct |
4 |
Correct |
219 ms |
53544 KB |
Output is correct |
5 |
Correct |
227 ms |
53640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
38748 KB |
Output is correct |
2 |
Correct |
5 ms |
38748 KB |
Output is correct |
3 |
Correct |
5 ms |
38748 KB |
Output is correct |
4 |
Correct |
6 ms |
38904 KB |
Output is correct |
5 |
Correct |
5 ms |
38748 KB |
Output is correct |
6 |
Correct |
5 ms |
38748 KB |
Output is correct |
7 |
Correct |
5 ms |
38744 KB |
Output is correct |
8 |
Correct |
5 ms |
38748 KB |
Output is correct |
9 |
Correct |
5 ms |
38748 KB |
Output is correct |
10 |
Correct |
5 ms |
38748 KB |
Output is correct |
11 |
Correct |
5 ms |
38748 KB |
Output is correct |
12 |
Correct |
5 ms |
38836 KB |
Output is correct |
13 |
Correct |
5 ms |
38748 KB |
Output is correct |
14 |
Correct |
6 ms |
38840 KB |
Output is correct |
15 |
Correct |
5 ms |
38748 KB |
Output is correct |
16 |
Correct |
5 ms |
38748 KB |
Output is correct |
17 |
Correct |
5 ms |
38748 KB |
Output is correct |
18 |
Correct |
7 ms |
39260 KB |
Output is correct |
19 |
Correct |
7 ms |
39472 KB |
Output is correct |
20 |
Correct |
7 ms |
39260 KB |
Output is correct |
21 |
Correct |
7 ms |
39260 KB |
Output is correct |
22 |
Correct |
8 ms |
39460 KB |
Output is correct |
23 |
Correct |
7 ms |
39256 KB |
Output is correct |
24 |
Correct |
8 ms |
39260 KB |
Output is correct |
25 |
Correct |
6 ms |
38948 KB |
Output is correct |
26 |
Correct |
8 ms |
39260 KB |
Output is correct |
27 |
Correct |
9 ms |
39472 KB |
Output is correct |
28 |
Correct |
8 ms |
39260 KB |
Output is correct |
29 |
Correct |
7 ms |
39260 KB |
Output is correct |
30 |
Correct |
8 ms |
39256 KB |
Output is correct |
31 |
Correct |
8 ms |
39260 KB |
Output is correct |
32 |
Correct |
9 ms |
39140 KB |
Output is correct |
33 |
Correct |
8 ms |
39260 KB |
Output is correct |
34 |
Correct |
81 ms |
59988 KB |
Output is correct |
35 |
Correct |
87 ms |
59924 KB |
Output is correct |
36 |
Correct |
81 ms |
59932 KB |
Output is correct |
37 |
Correct |
116 ms |
60056 KB |
Output is correct |
38 |
Correct |
132 ms |
59948 KB |
Output is correct |
39 |
Correct |
116 ms |
60024 KB |
Output is correct |
40 |
Correct |
117 ms |
60048 KB |
Output is correct |
41 |
Correct |
6 ms |
38744 KB |
Output is correct |
42 |
Correct |
6 ms |
38748 KB |
Output is correct |
43 |
Correct |
248 ms |
53620 KB |
Output is correct |
44 |
Correct |
219 ms |
53544 KB |
Output is correct |
45 |
Correct |
227 ms |
53640 KB |
Output is correct |
46 |
Correct |
229 ms |
57724 KB |
Output is correct |
47 |
Correct |
218 ms |
56660 KB |
Output is correct |
48 |
Correct |
227 ms |
58200 KB |
Output is correct |
49 |
Correct |
253 ms |
56660 KB |
Output is correct |
50 |
Correct |
308 ms |
53328 KB |
Output is correct |
51 |
Correct |
274 ms |
53072 KB |
Output is correct |
52 |
Correct |
278 ms |
53072 KB |
Output is correct |
53 |
Correct |
277 ms |
53076 KB |
Output is correct |