传送门:Atcoder - 165F
思路分析
回溯的时候删除该点的贡献就行了,然后就是用线段树优化lis的过程了
样例输入
10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10
样例输出
1
2
3
3
4
4
5
2
2
3
AC代码
#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
struct node {
int l,r,mid,ma;
} tree[N<<2];
int a[N],ans[N];
vector<int>e[N];
int n;
void pp(int x) {
tree[x].ma=max(tree[ls].ma,tree[rs].ma);
}
void build(int x,int l,int r) {
tree[x]= {l,r,l+r>>1,0};
if(l==r) {
return ;
}
int mid=tree[x].mid;
build(ls,l,mid);
build(rs,mid+1,r);
}
void modify(int x,int pos,int k) {
if(tree[x].l==pos && tree[x].r==pos) {
tree[x].ma=k;
return ;
}
int mid=tree[x].mid;
if(pos<=mid) modify(ls,pos,k);
if(pos>mid) modify(rs,pos,k);
pp(x);
}
int query(int x,int l,int r) {
if(l<=tree[x].l&&tree[x].r<=r) {
return tree[x].ma;
}
int mid=tree[x].mid;
int res=0;
if(l<=mid) res=max(res,query(ls,l,r));
if(r>mid) res=max(res,query(rs,l,r));
return res;
}
void dfs(int u,int fa) {
int now=a[u];
int sum=query(1,1,now-1)+1;
int pre=query(1,now,now);
modify(1,now,max(sum,pre));
ans[u]=query(1,1,n+5);
for(auto v:e[u]) {
if(v==fa) continue;
dfs(v,u);
}
modify(1,now,pre);
}
int main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
read(n);
vector<int>b;
for(int i=1; i<=n; i++) {
read(a[i]);
b.pb(a[i]);
}
sort(all(b));
b.erase(unique(all(b)),b.end());
for(int i=1; i<=n; i++) a[i]=lower_bound(all(b),a[i])-b.begin()+2;
for(int i=1; i<n; i++) {
int u,v;
read(u,v);
e[u].pb(v);
e[v].pb(u);
}
build(1,1,n+5);
dfs(1,1);
for(int i=1; i<=n; i++) cout<<ans[i]<<"\n";
return 0;
}