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

这道题一开始打算用树形dp,分别以每个点作为根做n次来进行,但是时间复杂度太高,后来决定用二分形式的最长上升子序列,但是如何处理同层的节点又成了新的问题,那么就保存现场,结束遍历后恢复现场
思路:
设dp[i]为长度为i+1时结尾的最小值,(初始化为inf),
1 二分形式的最长上升子序列,假设从根节点遍历到叶节点,从根节点到叶节点就有一个人口值序列,从根节点往叶节点,对每个节点,找到它可以作为dp[i]的位置,因为dp[i]的值一定是递增的(想想为什么),所以可以用二分来查找这个位置.然后更新,这个位置的最大值就是答案,这里时间复杂度是(nlogn)
2 上述说的是单链的情况,因为这个时候用来更新dp的都是位在它前面的点,所以一定是合法的,可是树上的点有同级的和不在一个枝上的,怎么办?(如果直接按照找出所有链然后计算,复杂度会高到n^3)这个时候就要利用到回溯法,把当前修改了的dp值先保存起来,遍历完这个节点再恢复,这里同前面时间复杂度结合起来(nlogn)
3 当根不同的时候,上升还是下降,还有能遍历到的链的长度都不一样,因为(nnlogn)复杂度也够,所以把每个点都当成一次根
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];//dp[i]i+1长度的结尾最小人口数,n 城市数,r[i] 第i个城市的人口数
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];//邻接表相关
int ans;
void dfs(int s,int f){
int pos=lower_bound(dp,dp+n,r[s])-dp;//找到一个比s人口值小的dp[]值
int tmp=dp[pos];//保存这个dp值
ans=max(pos+1,ans);
dp[pos]=min(dp[pos],r[s]);//更新dp
for(int i=first[s];i!=-1;i=nxt[i]){//继续往下延拓
int t=to[i];
if(t==f)continue;
dfs(t,s);
}
dp[pos]=tmp;//恢复
}
void addedge(int f,int t,int ind){
nxt[ind*2]=first[f];to[ind*2]=t;first[f]=2*ind;
nxt[ind*2+1]=first[t];to[ind*2+1]=f;first[t]=2*ind+1;
}
int main(){
scanf("%d",&n);
memset(first,-1,sizeof(first));
fill(dp,dp+n,inf);//初始化
for(int i=1;i<=n;i++)scanf("%d",r+i);//输入
for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t,i-1);}
for(int i=1;i<=n;i++){dfs(i,-1);}//开始分别以i为根遍历
printf("%d\n",ans);//输出
return 0;
}

  

cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1的更多相关文章

  1. ACM/ICPC 之 最长公共子序列计数及其回溯算法(51Nod-1006(最长公共子序列))

    这道题被51Nod定为基础题(这要求有点高啊),我感觉应该可以算作一级或者二级题目,主要原因不是动态规划的状态转移方程的问题,而是需要理解最后的回溯算法. 题目大意:找到两个字符串中最长的子序列,子序 ...

  2. hdu 5773 The All-purpose Zero 最长上升子序列+树状数组

    题目链接:hdu 5773 The All-purpose Zero 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的. 因此我们可以把0拿出来,对剩下的做O(nl ...

  3. BZOJ 3173 最长上升子序列(树状数组+二分+线段树)

    给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...

  4. bzoj3173: [Tjoi2013]最长上升子序列(树状数组+二分倒推)

    3173: [Tjoi2013]最长上升子序列 题目:传送门 题解:  好题! 怎么说吧...是应该扇死自己...看错了两次题: 每次加一个数的时候,如果当前位置有数了,是要加到那个数的前面,而不是直 ...

  5. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  6. 最长公共子序列(lcs)

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符 ...

  7. Codeforces 490F Treeland Tour 树上的最长上升子序列

    题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...

  8. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  9. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

随机推荐

  1. POJ - 3308 Paratroopers (最小点权覆盖)

    题意:N*M个格点,K个位置会有敌人.每行每列都有一门炮,能打掉这一行(列)上所有的敌人.每门炮都有其使用价值.总花费是所有使用炮的权值的乘积.求最小的总花费. 若每门炮的权值都是1,就是求最小点覆盖 ...

  2. 【android】开源一个企业通讯录app

    软件背景:该app不是替代手机通讯录,而是对其一种补充.项目只是通讯录客户端,数据源是访问本地.还是访问远程服务器,由你来实现 开源地址:http://git.oschina.net/yso/Smar ...

  3. 安卓 Activity 生命周期

    创建到销毁的生命周期: onCreate()->onStart()->onResume()->onPause->onStop->onDestroy 启动到前台在到后台的生 ...

  4. getAttribute() 与 attr() 的区别

    getAttribute() 和 attr() 都是获取元素属性的方法,只是一种是 JS 写法,一种是 JQ 写法,但其实它们是有区别的. 主要区别 调用 getAttribute() 的主体必须是元 ...

  5. c语言数据类型字节长度

    突然间就想到了long和int到底什么区别(发现有很多问题都是突然间想到的),然后百度.google各种查找,各种书籍:<C++ Primer>.<C程序设计语言>查看,终于明 ...

  6. 10个足以让你成为更优秀的程序员的C语言资源

    一些人觉得编程无聊,一些人觉得它很好玩.但每个程序员都必须紧跟编程语言的潮流.大多数程序员都是从C开始学习编程的,因为C是用来写操作系统.应用程序最常用的语言. · C编程笔记 这些是华盛顿实验学院C ...

  7. linux下挂载ISO像镜文件

    挂载命令(mount) 命令格式:mount [-t vfstype] [-o options] device dir其中:1.-t vfstype 指定文件系统的类型,通常不必指定.mount 会自 ...

  8. Windows下如何配置apache虚拟主机

    其实apache配置虚拟主机说简单也简单,但是就是就有几个坑,要是稍不注意就掉坑里了. --小树前言 坑三连 没遇到这三个坑,就配置得很顺畅了 用自己指定的域名进入不了任何页面. 只能进apache的 ...

  9. 记一次mogodb占用cpu高问题

    公司服务器上安装了contly,是一个开源的node.js项目,用于统计手机app使用情况,后端数据储存使用的mongodb,使用的时候经常发现mongodb占用cpu非常高,打到了210%的爆表值 ...

  10. Base64压缩UUID长度替换Hibernate原有UUID生成器

    本文来自http://my.oschina.net/noahxiao/blog/132277,个人储藏使用 1.背景 在采用Hibernate做对象映射时,我一直都采用UUID来做主键.由于Hiber ...