cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1
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.
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 aj, bj (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.
Print the number of cities where the "Road Accident" band will have concerts.
6
1 2 3 4 5 1
1 2
2 3
3 4
3 5
3 6
4
5
1 2 3 4 5
1 2
1 3
2 4
3 5
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; }