我的思路就是跟最长上升子序列的二分做法差不多的,但是每次把之前的修改回来,但是不知道为什么WA了一半/kk
#include <bits/stdc++.h>
using namespace std;
# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)
# define RepG(i,u) for(int i=head[u];~i;i=e[i].next)
typedef long long ll;
const int N=4e5+5;
template<typename T> void read(T &x){
x=0;int f=1;
char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
x*=f;
}
# define int long long
int n;
int a[N],b[N],sz;
int head[N],cnt;
int f[N];
int ans;
int d[N];
struct Edge{
int to,next;
}e[N<<1];
void add(int x,int y){
e[++cnt]=(Edge){y,head[x]},head[x]=cnt;
}
void dfs(int u,int fa)
{
int tmp=lower_bound(d,d+n,a[u])-d;
f[u]=tmp+1;
int t=d[tmp];
d[tmp]=a[u];
RepG(i,u){
int v=e[i].to;
if(v==fa)continue;
dfs(v,u);
}
d[tmp]=t;
}
signed main()
{
memset(d,0x3f,sizeof(d));
memset(head,-1,sizeof(head));
read(n);
Rep(i,1,n)read(a[i]),b[i]=a[i];
sort(b+1,b+n+1);
sz=unique(b+1,b+n+1)-b-1;
Rep(i,1,n)a[i]=lower_bound(b+1,b+sz+1,a[i])-b;
Rep(i,1,n-1){
int x,y;
read(x),read(y);
add(x,y),add(y,x);
}
// build(1,1,sz);
dfs(1,0);
Rep(i,1,n)printf("%lld\n",f[i]);
return 0;
}