转载请注明出处: 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

Problem Description
Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight wi. All the weights are distrinct.
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.
 
Input
The input consists of several tests. For each tests:
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.
 
Output
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
 
Sample Input
7
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7
 
Sample Output
5
 
YY一下,想到只要保证一棵子树的节点到其所有的叶子节点的路径都是满足递增的,找出一棵这样的最大的子树即可。
先从下往上dp一遍,然后再从上往下,把父亲的信息传送下来,嗯,然后就以O(n)得到了所需要的结果
不过直接dfs会爆栈,所以得开一下栈,然后用C++提交
 //#####################
//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)的更多相关文章

  1. hdu 5325 Crazy Bobo dfs

    // hdu 5325 Crazy Bobo // // 题目大意: // // 给你一棵树,树上每一个节点都有一个权值w,选择尽可能多的节点, // 这些节点相互联通,而且依照权值升序排序之后得到节 ...

  2. HDU 5325 Crazy Bobo(思路+dfs 记忆化)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  3. 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 ...

  4. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  5. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  6. hdu 5452 Minimum Cut 树形dp

    Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...

  7. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  8. hdu 1520Anniversary party(简单树形dp)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. Install Air Conditioning HDU - 4756(最小生成树+树形dp)

    Install Air Conditioning HDU - 4756 题意是要让n-1间宿舍和发电站相连 也就是连通嘛 最小生成树板子一套 但是还有个限制条件 就是其中有两个宿舍是不能连着的 要求所 ...

随机推荐

  1. PHP限制网页只能在微信内置浏览器中查看并显示

    微信现在算是火了,围绕微信开发的应用也越来越多了,前段时间,自己公司需要,用PHP写了一个微信应用,为了防止自己辛苦写成的PHP应用被盗用,于是 通过PHP做了限制,只能在微信自带的浏览器中才能打开本 ...

  2. C语言基础学习学习前的准备-1

    C语言概述 欢迎来到C的世界!C语言之所以命名为C,是因为C语言源自Ken Thompson发明的B语言.它是一种可移植语言,通常一个C程序可以经过很少的改动甚至不经改动就可以在其它系统上运行:它强大 ...

  3. C语言的画图(圆形动画)

    #include <stdio.h> #include <malloc.h>#include<graphics.h> #define LEN sizeof(stru ...

  4. 禁用与启用Button点击

    //启用查询按钮 btnFpSelect.setClickable(true); //禁用查询按钮 btnFpSelect.setClickable(false);

  5. BZOJ 2527 Meteors

    http://www.lydsy.com/JudgeOnline/problem.php?id=2527 思路:整体二分 #include<cstdio> #include<cmat ...

  6. Qt C++中的关键字explicit——防止隐式转换(也就是Java里的装箱),必须写清楚

    最近在复习QT,准备做项目了,QT Creator 默认生成的代码 explicit Dialog(QWidget *parent = 0)中,有这么一个关键字explicit,用来修饰构造函数.以前 ...

  7. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

  8. logstash 各种时间转换

    <pre name="code" class="html">日期格式转换: /***** nginx 访问日志 [elk@zjtest7-front ...

  9. Delphi 调试 通过BreakPoint

    1.打个断点, 如下图 2. 在断点上,邮件,如下图 3. 弹出一个窗体 ,如下图 在 condition 中写条件就可以了.  这样就可以按你假设的条件来进行了,方便.

  10. 面试题30.最小的k个数

    题目:输入n个整数,找出其中最小的k个数,例如输入4,5,1,6,2,7,3,8 这8个数字,则最小的四个数字为1,2,3,4, 这道题是典型的TopK问题,剑指Offer提供了两种方法来实现,一种方 ...