【bzoj 4919】大根堆(set启发式合并)

传送门biu~
假设是在序列上,就变成了nlogn的dp求最长上升子序列问题;假设是在树上,我们只需要在每个节点存下dp数组,然后用set的启发式合并将dp数组合并就可以了(代替Splay)。

#include<bits/stdc++.h>
#define N 200005
using namespace std;
vector<int>e[N];
multiset<int>s[N];
multiset<int>::iterator it;
int n,val[N];
void dfs(int x){
    for(int i=0;i<e[x].size();++i){
        dfs(e[x][i]);
        if(s[x]<s[e[x][i]])     swap(s[x],s[e[x][i]]);
        for(it=s[e[x][i]].begin();it!=s[e[x][i]].end();++it)    s[x].insert(*it);
    }
    it=s[x].lower_bound(val[x]);
    if(it!=s[x].end())  s[x].erase(it);
    s[x].insert(val[x]);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int x;
        scanf("%d%d",&val[i],&x);
        e[x].push_back(i);
    }
    dfs(1);
    printf("%d\n",s[1].size());
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zp1ng/article/details/79930742