hdu 5325 Crazy Bobo (树形dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Crazy Bobo
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1077 Accepted Submission(s): 326
A set with m nodes v1,v2,...,vm is a Bobo Set if:
- The subgraph of his tree induced by this set is connected.
- After we sort these nodes in set by their weights in ascending order,we get u1,u2,...,um,(that is,wui<wui+1 for i from 1 to m-1).For any node x in the path from ui to ui+1(excluding ui and ui+1),should satisfy wx<wui.
Your task is to find the maximum size of Bobo Set in a given tree.
The first line contains a integer n (1≤n≤500000). Then following a line contains n integers w1,w2,...,wn (1≤wi≤109,all the wi is distrinct).Each of the following n-1 lines contain 2 integers ai and bi,denoting an edge between vertices ai and bi (1≤ai,bi≤n).
The sum of n is not bigger than 800000.
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int Scan() {
int res=, ch;
while(ch=getchar(), ch<''||ch>'');
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return res;
}
void Out(int a) {
if(a>)
Out(a/);
putchar(a%+'');
}
int w[];
vector<int>G[];
int dp[];
void dfs1(int u,int fa){
dp[u] = ;
REP(i,G[u].size()){
int v = G[u][i];
if(v == fa)continue;
dfs1(v,u);
if(w[v] > w[u])dp[u] += dp[v];
}
}
void dfs2(int u,int fa){
if(fa!=-){
if(w[fa] > w[u])dp[u] += dp[fa];
}
REP(i,G[u].size()){
int v = G[u][i];
if(v == fa)continue;
dfs2(v,u);
}
}
int main()
{
//ios::sync_with_stdio(false);
int n;
while(scanf("%d",&n)!=EOF){
REP(i,n)w[i] = Scan();
REP(i,n)G[i].clear();
int u,v;
REP(i,n-){
u = Scan();
v = Scan();
u--;v--;
G[u].PB(v);
G[v].PB(u);
}
dfs1(,-);
dfs2(,-);
int ans = ;
REP(i,n)ans = max(dp[i],ans);
Out(ans);
puts("");
}
return ;
} /*
7
5 1 7 2 3 8 6
1 2
1 3
2 4
2 5
3 6
3 7
9
1 2 3 4 5 6 7 8 9
9 1
1 2
4 2
2 3
3 7
3 6
2 5
5 8
9
1 2 3 4 5 6 7 8 9
1 9
9 8
1 5
1 2
2 7
7 4
2 6
6 3
*/
hdu 5325 Crazy Bobo (树形dp)的更多相关文章
- hdu 5325 Crazy Bobo dfs
// hdu 5325 Crazy Bobo // // 题目大意: // // 给你一棵树,树上每一个节点都有一个权值w,选择尽可能多的节点, // 这些节点相互联通,而且依照权值升序排序之后得到节 ...
- HDU 5325 Crazy Bobo(思路+dfs 记忆化)
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Tota ...
- 2015 Multi-University Training Contest 3 hdu 5325 Crazy Bobo
Crazy Bobo Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- hdu 5452 Minimum Cut 树形dp
Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu 1520Anniversary party(简单树形dp)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Install Air Conditioning HDU - 4756(最小生成树+树形dp)
Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...
随机推荐
- Linux 编译安装httpsqs
wget http://httpsqs.googlecode.com/files/libevent-2.0.12-stable.tar.gz tar zxvf libevent-2.0.12-stab ...
- php用get_meta_tags轻松获取网页的meta信息
之前没发现php还有这个函数,get_meta_tags()直接就可以获取文件中meta标签的属性值,返回数组: <?php $metas = get_meta_tags('http://www ...
- Python新手学习基础之数据类型——数字类型
创建一组数字 Python 的有以下几种内置数字类型: int,整型,比如:1.-2.598: float,浮点型,比如:0.0.-3.5.18.55: bool,布尔型,即True和False两个关 ...
- GO 输出字符数同时输出这个字符串的字节数
package main import ( "fmt" "unicode/utf8" ) func main(){ var str string str=&qu ...
- Responder一点也不神秘————iOS用户响应者链完全剖析
一.事件分类 对于IOS设备用户来说,他们操作设备的方式主要有三种:触摸屏幕.晃动设备.通过遥控设施控制设备.对应的事件类型有以下三种: 1.触屏事件(Touch Event) 2.运动事件(Moti ...
- Cmake,链接一个外部(也可能是第三方,也可能是自己编译的)库
相当于设置VS工程里面的: 然后,为了链接成可执行文件,链接器就会到指定的目录寻找相应的库了. 以下时Demo: cmake_minimum_required(VERSION 2.8) #set(CM ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
- java_重写与重载的区别
重写与重载的区别 重载(Overloading)和重写(Overriding)是Java中两个比较重要的概念.但是对于新手来说也比较容易混淆.本文通过两个简单的例子说明了他们之间的区别. 定义 重载 ...
- java 正则表达式获取值
@Test public void testtest() { String test = "hahahhehe sendCode\":\"12367890123rsdfs ...
- vbox下centos安装增加功能失败
一般都是:unable to find the sources of your current Linux kernel. 先尝试这个吧:yum install kernel kernel-heade ...