枚举根+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. strcpy_s与strcpy对照

    strcpy_s和strcpy()函数功能几乎相同.strcpy函数.就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它仅仅能假定缓冲足够大来容纳要拷贝的字符串.在程序执行时,这将导致不可 ...

  2. hdu 2049 别easy列(4)——测试新郎

    问题: 使用double定义的数量和long数定义19-20出现分歧,原因不明.求大公社. 这个问题需要用long,否则,只是通过,这应该纠结了很久. 问题是乘以一个交错的思想相结合. 不easy系列 ...

  3. 重新想象 Windows 8 Store Apps (21) - 动画: ThemeTransition(过渡效果)

    原文:重新想象 Windows 8 Store Apps (21) - 动画: ThemeTransition(过渡效果) [源码下载] 重新想象 Windows 8 Store Apps (21) ...

  4. Java自定义比较器Comparator

    1.数字排序  奇数在前,偶数在后.奇数降序,偶数升序.输入:"0,1,2,3,4,5,6,7,8,9"        输出:"9,7,5,3,1,0,2,4,6,8&q ...

  5. Jenkins详细安装与构建部署使用教程(转)

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括:1.持续的软件版本发布 ...

  6. C#改动文件或目录的权限,为指定用户、用户组加入全然控制权限

    C#改动文件或文件夹的权限,为指定用户.用户组加入全然控制权限 //给Excel文件加入"Everyone,Users"用户组的全然控制权限 FileInfo fi = new F ...

  7. sublime配置攻略

    大家好,今天给大家分享的编辑器:sublime text2     我用过非常多编辑器, EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.V ...

  8. 抓取csdn上的各类别的文章 (制作csdn app 二)

    转载请表明出处:http://blog.csdn.net/lmj623565791/article/details/23532797 这篇博客接着上一篇(Android 使用Fragment,View ...

  9. socket计划编制的原则

    socket编程原理 1.问题的引入 1) 普通的I/O操作过程: UNIX系统的I/O命令集,是从Maltics和早期系统中的命令演变出来的,其模式为打开一读/写一关闭(open-write-rea ...

  10. 如何插上U盘 自动复制内容

    U盘插入自动复制,有效对付那些不肯给PPT的老师,还有一定几率拿到期末考试卷子···有图有真相!业界良心的好东西!!现在看还来得及!!! 代码优化了一下,把不是很重要的都删掉了,这次使用时看不出任何现 ...