枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ......

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 islarger 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

/* ***********************************************
Author :CKboss
Created Time :2015年03月14日 星期六 20时52分26秒
File Name :CF490F.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int maxn=6600; int n,val[maxn]; int Adj[maxn],Size;
struct Edge
{
int to,next;
}edge[maxn*2]; void init_edge()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
Adj[u]=Size++;
} int range[maxn],rn;
int dp[maxn],ans=1; void dfs(int u,int fa)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==fa) continue;
/// getLIS
int oldV; bool oldRn=false;
int POS=lower_bound(range,range+rn,val[v])-range;
oldV=range[POS]; range[POS]=val[v];
dp[v]=max(dp[v],POS+1);
if(POS==rn) { oldRn=true; rn++; } dfs(v,u); if(oldRn) rn--;
range[POS]=oldV;
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); init_edge();
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",val+i); for(int i=1;i<=n-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v); add_edge(v,u);
} /// enum root
for(int rt=1;rt<=n;rt++)
{
rn=0; range[rn++]=val[rt];
dp[rt]=max(dp[rt],1);
dfs(rt,0);
}
for(int i=1;i<=n;i++) ans=max(ans,dp[i]);
cout<<ans<<endl;
return 0;
}

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

Codeforces 490F. Treeland Tour 暴力+LIS的更多相关文章

  1. Codeforces 490F Treeland Tour(离散化 + 线段树合并)

    题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...

  2. Codeforces 490F Treeland Tour 树形dp

    Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...

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

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

  4. cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1

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

  5. Codeforces 667D World Tour 最短路

    链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...

  6. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

  7. Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

    D. World Tour   A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...

  8. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

  9. codeforces 897A Scarborough Fair 暴力签到

    codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...

随机推荐

  1. AIX 7.1 install python

    周围环境AIX7.1   设备python-2.6.2  因为互联网是非常多的安装文档.而且也没有细挑的版本号.因为我觉得python2.6 可能相对保守一些,至少之前用到的版本号是这个.所以此处依旧 ...

  2. strategy pattern

  3. 在borland c++ builder 中使用 google test (gtest)

    google test version: 1.6 c++ builder version: xe6 1 download google test 1.6 2 unzip the zip file. T ...

  4. 设计模式 - 出厂模式(factory pattern) 详细说明

    出厂模式(factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27081511 工厂方法模式 ...

  5. UVA 11149 - Power of Matrix(矩阵乘法)

    UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...

  6. 第三篇——第二部分——第三文 配置SQL Server镜像——域环境

    原文:第三篇--第二部分--第三文 配置SQL Server镜像--域环境 原文出处:http://blog.csdn.net/dba_huangzj/article/details/28904503 ...

  7. SplashScreenDemo

    对Java应用最常见的抱怨就是启动时间太长.这是因为Java虚拟机花费一段时间去加载所有必需的类,特别是对Swing应用,它们需要从Swing和AWT类库代码中去抽取大量的内容. 用户并不喜欢应用程序 ...

  8. HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分法+枚举)

    主题链接: HDU:pid=4430" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4430 ...

  9. 第七章——DMVs和DMFs(4)——用DMV和DMF监控磁盘IO

    原文:第七章--DMVs和DMFs(4)--用DMV和DMF监控磁盘IO 前言: 本文为本系列最后一篇,作为DBA,你必须经常关注磁盘的I/O问题,一旦出现问题,要尽快分析出是什么问题.SQLServ ...

  10. 【转】Android 避免APP启动闪黑屏(Theme和Style)

    前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受.没办法,只能想想怎么解决,最后找到了下面的方法.闪黑屏的原因主要是我们启动Activity的时候,需要跑完onCreate和on ...