Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ......
/* ***********************************************
Author :CKboss
Created Time :2015年03月14日 星期六 20时52分26秒
File Name :CF490F.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int maxn=6600;
int n,val[maxn];
int Adj[maxn],Size;
struct Edge
{
int to,next;
}edge[maxn*2];
void init_edge()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
}
void add_edge(int u,int v)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
}
int range[maxn],rn;
int dp[maxn],ans=1;
void dfs(int u,int fa)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
/// getLIS
int oldV; bool oldRn=false;
int POS=lower_bound(range,range+rn,val[v])-range;
oldV=range[POS]; range[POS]=val[v];
dp[v]=max(dp[v],POS+1);
if(POS==rn) { oldRn=true; rn++; }
dfs(v,u);
if(oldRn) rn--;
range[POS]=oldV;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init_edge();
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",val+i);
for(int i=1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v); add_edge(v,u);
}
/// enum root
for(int rt=1;rt<=n;rt++)
{
rn=0; range[rn++]=val[rt];
dp[rt]=max(dp[rt],1);
dfs(rt,0);
}
for(int i=1;i<=n;i++) ans=max(ans,dp[i]);
cout<<ans<<endl;
return 0;
}
版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss
浙公网安备 33010602011771号