Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ......
5 seconds
256 megabytes
standard input
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.
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
/* ***********************************************
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的更多相关文章
- Codeforces 490F Treeland Tour(离散化 + 线段树合并)
题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...
- Codeforces 490F Treeland Tour 树形dp
Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...
- Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...
- cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1
F. Treeland Tour time limit per test 5 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- 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 ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
随机推荐
- 小强的HTML5移动开发之路(50)——jquerymobile页面初始化过程
为了方便说明和更加直观的展示jquerymobile的页面初始化过程以及各个事件的触发过程,我绘制了一幅流程图: 图中用红色框圈起来的是界面中的事件,測试代码例如以下: <!DOCTYPE ht ...
- kernel 于ioctl申请书
ioctl经营无纸装置频繁使用的类型.同时这是一个非常实用的方法进程调试. 这里正在进行wdt该文章继续 static long at91_wdt_ioctl(struct file *file, u ...
- Unity3d该物业(Attributes)整理
http://blog.sina.com.cn/s/blog_5b6cb9500101857b.html Attributes属性属于U3D的RunTimeClass,所以加上下面的命名空间是必须的了 ...
- [LeetCode238]Product of Array Except Self
题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 同ListView该接口无法通过手势滑动左右切换界面问题解决方法
同ListView该接口无法通过手势滑动左右切换界面问题解决方法 问题描写叙述: 在做OnGestureListener滑动切换窗体的时候,会遇到这种问题.就是当界面中含有ListView的时候.On ...
- ThinkPad E530 Fedora 20 无线上网问题
它一直在使用 Fedora 家庭 Linux. 但它自带的无线网卡驱动似下一些问题,通常,有时连接,有时你不能. 经常搜索不到的家用无线路由器. 因为家里有网线所以一直没有在意.没什么事就折腾了一下. ...
- 读书时间《JavaScript高级程序设计》四:BOM,客户端检测
隔了一段时间,现在开始看第8章. 第8章:BOM BOM提供了很多对象,用于访问浏览器的功能.BOM的核心对象是window,它表示浏览器的一个实例. window对象是通过javascript访问浏 ...
- Linux下 高性能、易用、免费的ASP.NET服务器
Linux下 高性能.易用.免费的ASP.NET服务器 http://www.jexus.org/#
- 猫学习IOS(四)UI半小时就搞定Tom猫
阿土 首先对影响 下载项目的源材料: Tom猫游戏代码iOS 素材http://blog.csdn.net/u013357243/article/details/44457357 效果图 以前风靡一时 ...
- 【原创】构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)
原文:[原创]构建高性能ASP.NET站点之一 剖析页面的处理过程(前端) 构建高性能ASP.NET站点之一 剖析页面的处理过程(前端) 前言:在对ASP.NET网站进行优化的时候,往往不是只是懂得A ...