题意:
有一棵n个节点的二叉树,1为根节点,每个节点有一个值wi。现在要选出尽量多的点。
对于任意一棵子树,都要满足:
如果选了根节点的话,在这棵子树内选的其他的点都要比根节点的值大;
如果在左子树选了一个点,在右子树中选的其他点要比它小。
思路:
按照根节点-右子树-左子树的顺序建立dfs序列,然后求lis即可
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional> #define fst first #define sc second #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std; typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL; const db eps = 1e-6; const int mod = 1e9+7; const int maxn = 2e6+100; const int maxm = 2e6+100; const int inf = 0x3f3f3f3f; const db pi = acos(-1.0); int a[maxn]; vector<int>v; int l[maxn],r[maxn]; void dfs(int x){ v.pb(a[x]); if(r[x])dfs(r[x]); if(l[x])dfs(l[x]); return; } int low[maxn],f[maxn]; int main() { int n; v.pb(0); scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%d", &a[i]); } for(int i = 1; i <= n; i++){ scanf("%d %d", &l[i], &r[i]); } dfs(1); int cnt = 0; mem(low,-1); for(int i = 1; i <= n; i++){ int t; t = lower_bound(low+1,low+1+cnt,a[i])-low; f[i]=t; cnt=max(cnt,t); low[t]=a[i]; } return 0; }