codeforces 490F Treeland Tour(dp)

题目链接

F. Treeland Tour
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The "Road Accident" band is planning an unprecedented tour around Treeland. The RA fans are looking forward to the event and making bets on how many concerts their favorite group will have.

Treeland consists of n cities, some pairs of cities are connected by bidirectional roads. Overall the country has n - 1 roads. We know that it is possible to get to any city from any other one. The cities are numbered by integers from 1 to n. For every city we know its value ri — the number of people in it.

We know that the band will travel along some path, having concerts in some cities along the path. The band's path will not pass one city twice, each time they move to the city that hasn't been previously visited. Thus, the musicians will travel along some path (without visiting any city twice) and in some (not necessarily all) cities along the way they will have concerts.

The band plans to gather all the big stadiums and concert halls during the tour, so every time they will perform in a city which population is larger than the population of the previously visited with concert city. In other words, the sequence of population in the cities where the concerts will be held is strictly increasing.

In a recent interview with the leader of the "road accident" band promised to the fans that the band will give concert in the largest possible number of cities! Thus the band will travel along some chain of cities of Treeland and have concerts in some of these cities, so that the population number will increase, and the number of concerts will be the largest possible.

The fans of Treeland are frantically trying to figure out how many concerts the group will have in Treeland. Looks like they can't manage without some help from a real programmer! Help the fans find the sought number of concerts.

Input

The first line of the input contains integer n (2 ≤ n ≤ 6000) — the number of cities in Treeland. The next line contains n integersr1, r2, ..., rn (1 ≤ ri ≤ 106), where ri is the population of the i-th city. The next n - 1 lines contain the descriptions of the roads, one road per line. Each road is defined by a pair of integers ajbj (1 ≤ aj, bj ≤ n) — the pair of the numbers of the cities that are connected by the j-th road. All numbers in the lines are separated by spaces.

Output

Print the number of cities where the "Road Accident" band will have concerts.

Sample test(s)
input
6
1 2 3 4 5 1
1 2
2 3
3 4
3 5
3 6
output
4
input
5
1 2 3 4 5
1 2
1 3
2 4
3 5
output
3

题意:n个结点的树,每个点有点权。对于树上的所有路径来说,每条路径都有一个最长上升子序列,求所有路径的最长上升子序列中最长的上升子序列的长度?

题解:官方题解是O(N^2)的算法,暂时还不会。还有O(N*lgN*lgN)的算法,貌似要用启发式合并,暂时不会。

我只会O(N^2*lgN)的算法。

枚举起点x,求以x为起点的路径的最长上升子序列。以x为起点遍历整科树。我们可以在遍历树的过程中动态维护当前路径上的点的最长上升子序列。

具体来说,我们用dp[ i ] 表示当前路径上,长度为i的最长上升子序列最后一位最小为多少。在遍历树的过程中,只有两种情况,在路径的最后加入一个点,在路径的最后删除一个点。加入一个点我们可以在O(lgn)的复杂度下维护dp数组,删除一个点我们可以做到O(1)维护dp数组。复杂度O(n^2*lgn)。

详情见代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<vector>
#define inff 0x3fffffff
#define nn 6100
#define mod 1000000007
typedef long long LL;
const LL inf64=inff*(LL)inff;
using namespace std;
int n;
int val[nn];
struct node
{
    int en,next;
}E[nn*2];
int p[nn],num;
void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}
void add(int st,int en)
{
    E[num].en=en;
    E[num].next=p[st];
    p[st]=num++;

    E[num].en=st;
    E[num].next=p[en];
    p[en]=num++;
}
int ans;
int dp[nn][nn];
int ldp[nn];
int ls;
int wei[nn];
void jia(int id)
{
    int l=0,r=ls;
    int mid;
    while(l<r)
    {
        mid=(l+r+1)/2;
        if(dp[mid][ldp[mid]]<val[id])
        {
            l=mid;
        }
        else
            r=mid-1;
    }
    if(val[id]<=dp[l+1][ldp[l+1]])
    {
        ldp[l+1]++;
        dp[l+1][ldp[l+1]]=val[id];
        ans=max(ans,l+1);
        wei[id]=l+1;
    }
    ls++;
}
void san(int id)
{
<pre name="code" class="cpp">    if(wei[id]!=-1)
        ldp[wei[id]]--;
    wei[id]=-1;
    ls--;
}void dfs(int id,int fa){ jia(id); int i,w; for(i=p[id];i+1;i=E[i].next) { w=E[i].en; if(w==fa) continue; dfs(w,id); } san(id);}int main(){ int i,u,v; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&val[i]); } init(); for(i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); } ans=0; memset(ldp,0,sizeof(ldp)); memset(wei,-1,sizeof(wei)); for(i=1;i<=n;i++) dp[i][0]=inff; dp[0][0]=-inff; ls=0; for(i=1;i<=n;i++) { dfs(i,i); } printf("%d\n",ans); } return 0;}

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值