题意:
一棵树(6000个节点)每个点上有个值 对于它里面的每一条路径可以用路径上的点的值的LIS来表示 问 这样的LIS最长有多长
思路:
枚举节点当开头复杂度O(n) 在树里面做LIS复杂度O(nlogn) 总共O(n^2logn)
应该没有难度 做LIS的时候类似线性序列 用栈维护 dfs时候记得回溯就好
PS:正解应该不是这样的 不过数据好小
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
#define N 6010
int n, ans = 1;
int head[N], tot;
struct edge {
int v, next;
} ed[N << 1];
int val[N], st[N], top;
void add(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
int org, pos = -1;
if (!top || st[top - 1] < val[u]) {
st[top++] = val[u];
ans = max(ans, top);
} else {
pos = lower_bound(st, st + top, val[u]) - st;
org = st[pos];
st[pos] = val[u];
}
for (int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if (v != fa)
dfs(v, u);
}
if (pos == -1)
top--;
else
st[pos] = org;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &val[i]);
head[i] = -1;
}
for (int i = 2; i <= n; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
for (int i = 1; i <= n; i++) {
top = 0;
dfs(i, i);
}
printf("%d\n", ans);
return 0;
}