感受

思路

图片说明






#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;//1061109567 大约1e9
const ll INF = 0x3f3f3f3f3f3f3f3f;//4557430888798830399 大约4e18 且INF + INF < long long最大值
const int maxn = 1e5 + 10;
struct edge{
    int v, nex;
}e[maxn << 1];
int n, head[maxn], cnt;
ll w[maxn];
vector<ll> a;
void init(){
    cnt = 0;
    for(int i = 1; i <= n; i++){
        head[i] = -1;
    }
}
void add_edge(int u, int v){
    e[cnt] = (edge){v, head[u]};
    head[u] = cnt++;
}
void dfs(int u){
    a.push_back(w[u]);
    int v;
    for(int i = head[u]; ~i; i = e[i].nex){
        v = e[i].v;
        dfs(v);
    }
}
ll dp[maxn];
int solve(){
    memset(dp, INF, sizeof(dp));
    for(int i = 0; i < n; i++){
        *lower_bound(dp, dp + n, a[i]) = a[i];
    }
    return lower_bound(dp, dp + n, INF) - dp;
}
int main(){
    scanf("%d", &n);
    init();
    int x, y;
    for(int i = 1; i <= n; i++) scanf("%lld", &w[i]);
    for(int i = 1; i <= n; i++){
        scanf("%d%d", &x, &y);
        if(x) add_edge(i, x);
        if(y) add_edge(i, y);
    }
    dfs(1);
    printf("%d\n", solve());
    return 0;
}