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 思路: 暴力大法好 代码: ...
随机推荐
- JAVA学习课第二十八届(多线程(七))- 停止-threaded多-threaded面试题
主密钥 /* * wait 和 sleep 差别? * 1.wait能够指定时间也能够不指定 * sleep必须指定时间 * 2.在同步中,对CPU的运行权和锁的处理不同 * wait释放运 ...
- cgo 随笔(golang)
结构体应用 //结构体定义如下 // test.h struct test { int a; int b; int c; } 在golang中的调用如下: package name import &q ...
- 利用Eclipse中的Maven构建Web项目(三)
利用Eclipse中的Maven构建Web项目 1.将Maven Project转换成动态Web项目,鼠标右键项目,输入"Project Facets" 2.依据Dynamic W ...
- Android - 分享内容 - 给其他APP发送内容
创建一个intent时,必须要指定intent将要触发的操作.Android定义了很多操作,包括ACTION_SEND,就象可以猜到的一样,表示intent是把数据从一个activity发送给另一个, ...
- 由<a href = "#" > 引发的思考
原文:由<a href = "#" > 引发的思考 前阵子在一个移动项目中,通过 <a href = "#" > 的方式 绑定clic ...
- 【Android进阶】快捷图标的创建与移除
注释已经说得很清楚了,如果有疑问,请留言 /** * 添加桌面快捷方式 * * @param view */ public void click1(View view) { if (isExit()) ...
- ARM装配说明MCR/MRC学习
MCR指令ARM数据寄存器传送到协处理器寄存器.假设协处理器不能成功运行操作.会产生未定义指令中止. 语法教学格式: MCR{<cond>} p15, 0, <Rd>, < ...
- Cocos2dx-3.1.1 冒险01----> 文件夹结构、新项目project创建并执行
windows开发环境:window7.vs2012.python2.7.6 Cocos2d-x 3.1.1的完整文件夹例如以下:比起曾经的2.x的版本号来说分类更规范了 watermark/2/te ...
- nodejs 复制、移动文件
对路径没有做验证 复制文件 var fs = require('fs'); var path = require('path'); var fileName = "coverflow-3.0 ...
- onethink和phpwind共享
将onethink和phpwind数据库安装在一起.使用公用表前缀. 将onethink的member表点phpwind有user表 这是onethink在根文件夹的安装,phpwind安装在bbs的 ...