选点
题解: 很明显的最大上升子序列问题,只不过n的范围是1e5 ,要用O(nlogn) 的LIS求法;
#include<bits/stdc++.h>
#define ft first
#define sd second
using namespace std;
const int MAX = 1e5+7;
pair<int,int> G[MAX];
int n,num=0;
int w[MAX];
int low[MAX];
int sq[MAX];
void DFS(int x)
{
if(x<=0) return ;
sq[++num] = w[x]; //根
DFS(G[x].sd); //右
DFS(G[x].ft); //左
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>w[i];
for(int i=1;i<=n;i++) cin>>G[i].ft>>G[i].sd;
DFS(1);
int ans = 1;
low[ans] = sq[1];
for(int i=2;i<=num;i++){
//cout<<sq[i];
if(sq[i] > low[ans]) low[++ans] = sq[i];
else {
int j = lower_bound(low+ 1,low+ans+ 1,sq[i]) - low;
low[j] = sq[i];
}
}
cout<<ans<<endl;
return 0;
}