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. HDU 4746 Mophues(莫比乌斯反演)

    题意:求\(1\leq i \leq N,1\leq j \leq M,gcd(i,j)\)的质因子个于等于p的对数. 分析:加上了对质因子个数的限制. 设\(f(d):[gcd(i,j)=d]\) ...

  2. Winter-2-STL-D The Blocks Problem 解题报告及测试数据

    Time Limit:3000MS     Memory Limit:0KB Description Background Many areas of Computer Science use sim ...

  3. Docker+.Net Core 的那些事儿-4.还有这种操作!?

    1.通过docker run -v命令映射工作目录 通过一系列上述操作,我们可以发现我们的发布是基于镜像的,也就是说,在后期的迭代过程中,如果有些代码修改,我们就不得不删除旧的容器和镜像,dotnet ...

  4. jquery ajax修改全局变量或者局部变量示例代码

    今天在工作的时候遇见一个问题,利用ajax到action中查询返回的值付给全局变量或者局部变量,总是改变不了,后来查找资料才发现需要添加async:false 示例代码: var status=1; ...

  5. 利用python获取nginx服务的ip以及流量统计信息

    #!/usr/bin/python #coding=utf8 log_file = "/usr/local/nginx/logs/access.log" with open(log ...

  6. Hive Shell常用操作

    1.Hive非交互模式常用命令: 1) hive -e:从命令行执行指定的HQL,不需要分号: % hive -e 'select * from dummy' > a.txt 2) hive – ...

  7. 如何使一个openwrt下的软件开机自启动

    条件有三: 1.需要在软件包的Makefile中添加宏定义Package/$(package-name)/preinst和Package/$(package-name)/prerm define Pa ...

  8. NOIP树上问题总结

    这几年考了好几次树上问题: NOIP2012 疫情控制(二分答案+倍增+贪心) NOIP2013 货车运输(最大生成树+倍增) NOIP2014 联合权值(勉强算作树形dp的傻逼题) NOIP2015 ...

  9. java基础笔试题二(集合关系)

    知识点:java集合继承关系(Collection,Map) 1.集合框架体系图 2.java的集合层次 来自博客(http://blog.csdn.net/stubbornaccepted/arti ...

  10. 动态规划入门-01背包问题 - poj3624

    2017-08-12 18:50:13 writer:pprp 对于最基础的动态规划01背包问题,都花了我好长时间去理解: poj3624是一个最基本的01背包问题: 题意:给你N个物品,给你一个容量 ...