题目链接:
题意:告诉一个无向无环图,然后求在联通的路上的lis。
思路:枚举起点求lis 复杂度是n^2logn,貌似这复杂度对时间有点玄,估计是数据有点弱。。。
首先枚举起点,然后求lis,主要是用dfs求的,要用到回溯的思想,我觉得是只要更新了,就要保存那个操作,然后一旦这一次的搜索完成,那么就要立即回复g数组的值,因为有很多不同的路线,所以一条路走完后,就要回复以前的状态哦,免得影响另外一条路。。我觉得尽管他们都说是暴力,但是我觉得这个题还是蛮好的。。。
题目:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
priority_queue<int,vector<int>,greater<int> >Q;
const int maxn=6000+10;
struct Egde
{
int next,to;
}edge[maxn<<1];
int g[maxn],val[maxn],head[maxn],cnt,n,ans,top;
void add_edge(int x,int y)
{
edge[++cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt;
}
void dfs(int u,int top=0,int fa=0)
{
int tmp,pos;
if(top==0||val[u]>g[top])
{
pos=++top;
tmp=g[top];
g[top]=val[u];
ans=max(ans,top);
}
else
{
pos=lower_bound(g+1,g+1+top,val[u])-g;
tmp=g[pos];
g[pos]=val[u];
}
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v!=fa)
dfs(v,top,u);
}
g[pos]=tmp;
}
int main()
{
int x,y;
while(~scanf("%d",&n))
{
ans=cnt=0;
memset(head,-1,sizeof(head));
memset(g,-1,sizeof(g));
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
for(int i=1;i<=n;i++)
dfs(i);
printf("%d\n",ans);
}
return 0;
}