Crazy Bobo

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 612    Accepted Submission(s): 189

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
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  

pid=5326" target="_blank" style="color:rgb(26,92,200); text-decoration:none">5326 5324 5323 5322 5321 

 

/*

參考此人博客 :http://www.mamicode.com/info-detail-948802.html
记得用c++交 */ #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
using namespace std; #define N 800005 vector<int>g[N];
int n;
int ans[N];
int a[N]; int dfs(int u)
{
if(ans[u]) return ans[u];
ans[u]=1;
for(int i=0;i<g[u].size();i++)
{
int to=g[u][i];
ans[u]+=dfs(to);
}
return ans[u];
} int main()
{
int i,j;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
g[i].clear();
memset(ans,0,sizeof(ans));
int u,v;
i=n-1;
while(i--)
{
scanf("%d%d",&u,&v);
if(a[u]<a[v]) g[u].push_back(v);
else g[v].push_back(u);
}
int temp=0;
for(i=1;i<=n;i++)
{
temp=max(temp,dfs(i));
}
printf("%d\n",temp);
}
return 0;
}

HDU 5325 Crazy Bobo(思路+dfs 记忆化)的更多相关文章

  1. hdu 5325 Crazy Bobo dfs

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

  2. hdu 5325 Crazy Bobo (树形dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Crazy Bobo Time Limit: 6000/3000 MS (Java ...

  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. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  6. poj1088-滑雪 【dfs 记忆化搜索】

    http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 79806 ...

  7. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  8. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  9. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

随机推荐

  1. mongodb GUI工具

    人性化,界面工具 网上搜索找的一些 1. 官方网站 tools 2. adminMongo 这个也是我在使用的 3. MongoClient 4. NoSQL Manager for MongoDB ...

  2. Change the color of a link in an NSMutableAttributedString

    Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...

  3. CAD使用GetxDataString读数据(网页版)

    主要用到函数说明: MxDrawEntity::GetxDataString2 读取一个字符扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, retval ...

  4. Linux安装redis并且连接内网的redis

    1.安装redis步骤 1.首先准备工作  [root@10-100-14-130 ~]# yum install gcc-c++   yum install wget 2.推荐进入到linux路径/ ...

  5. 电子笔记本的思考(1)(ver0.2)

    章节:电子笔记本的思考(1)   陶哲轩在<解题·成长·快乐——陶哲轩教你学数学>中着重强调,用纸笔来“缓存”思维对于数学解题的重要性: 用选定的符号表达你所知道的信息,并画一个示意图.把 ...

  6. 【2018百度之星初赛(A)】1002 度度熊学队列

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6375 Knowledge Point: STL - map:https://www.cnblogs.c ...

  7. 服务器做ssh免秘钥登陆

    集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...

  8. python_ 学习笔记(hello world)

    python中的循环语句 循环语句均可以尾随一个else语句块,该块再条件为false后执行一次 如果使用break跳出则不执行. for it in [1,2,3,4]: print(it,end= ...

  9. 创建sum求多元素的和

    a = [1, 2, 3] b = [4, 5, 6] def sum_super(* args): s = 0 for i in args: s += sum(i) return s # print ...

  10. 第八节:web爬虫之urllib(四)

    第三个 模块parse : 是一个工具模块,提供了许多 URL 处理方法,比如拆分.解析.合并等等的方法.