https://ac.nowcoder.com/acm/contest/368/B
有一棵n个节点的二叉树,1为根节点,每个节点有一个值wi。现在要选出尽量多的点。
对于任意一棵子树,都要满足:
如果选了根节点的话,在这棵子树内选的其他的点都要比根节点的值大;
如果在左子树选了一个点,在右子树中选的其他点要比它小。
即为按照根节点-》右子树-》左子树的顺序选出最长上升子序列即可
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 4;
int rgt[maxn], lft[maxn];
int n, tot, dfsarr[maxn], lis[maxn], w[maxn], q[maxn];
void dfs(int pos) {
dfsarr[++tot] = pos;
if(rgt[pos]) dfs(rgt[pos]);
if(lft[pos]) dfs(lft[pos]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> w[i];
for(int i = 1; i <= n; i++)
cin >> lft[i] >> rgt[i];
dfs(1);
for(int i = 1; i <= n; i++) q[i] = w[dfsarr[i]];
int ltot = 1;
lis[1] = q[1];
for(int i = 2; i <= n; i++) {
if(q[i] > lis[ltot]) lis[++ltot] = q[i];
else {
int pos = lower_bound(lis + 1, lis + ltot + 1, q[i]) - lis;
lis[pos] = q[i];
}
}
cout << ltot << endl;
}