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. android系统和ios系统是如何实现推送的,ios为什么没有后台推送

    ios系统为什么没有后台推送? iOS 为了真正地为用户体验负责,不允许应用在后台活动.有了这个限制,但是对于终端设备,应用又是有必要“通知”到达用户的,随时与用户主动沟通起来的(典型的如聊天应用). ...

  2. The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567

    地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...

  3. NGUI制作 《九宫格》 图片

    什么是九宫格图片? 就是一张图片的上下左右四个角是固定的,无论X/Y被拉伸多大,图片都不会失真 效果图 ------> 那在NGUI里面怎么做到呢 1 首先你要把图片添加到NGUI图集里,点击E ...

  4. H5 播放视频常见bug及解决方案

    本文摘自:本文来自“小时光茶社(Tech Teahouse)”公众号 原文:https://mp.weixin.qq.com/s/MM5ZwCiWLAeHalsNYMImnw 1. 自动播放问题 通过 ...

  5. [简明版] 有道云笔记Markdown指南

    使用有道词典配合Markdown,可以快速准确做出美观精致的笔记,下面我们来看一下如何使用有道词典的MarkDown功能. 什么是Markdown?Markdown是一种轻量级的「标记语言」,通常为程 ...

  6. 安装centos7最小化安装

    分享一篇不错的文章: https://www.howtoforge.com/tutorial/centos-7-server/

  7. Hive常见问题

    1.HQL是否区分大小写 不区分 hive> select AGE from default.studeNT; --不区分大小写,即使是表中字段 2.查看创建表过程 show create ta ...

  8. Nagios监控mysql主从复制

    因为公司的nagios用了很久监控项目很多,也在zabbix迁移中,也就先临时用nagios监控mysql主从了 mysql> show slave status\G 查看其输出,即可判定主从复 ...

  9. jackson序列化字段字母大小写及字段名重复

    一:Jackson默认的属性发现规则将会查找到如下所述的属性: 1.所有被public修饰的字段(成员变量): 2.所有被public修饰的getter(即形如“getXxx()”的方法): 3.所有 ...

  10. Vue.js项目部署在Tomcat服务器上

    1.在本地的Vue框架中 执行npm run build  将我们的项目打包到dist 文件夹中 2.在服务器上的Tomcat的 webapps文件夹下,新建一个文件夹如:frontvue 3.启动t ...