HDU5325——DP+搜索——Crazy Bobo
2015-07-29 13:57
344 查看
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
[align=left]Sample Input[/align]
7
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7
[align=left]Sample Output[/align]
5
[align=left]Source[/align]
2015 Multi-University Training Contest 3
[align=left]Recommend[/align]
wange2014 | We have carefully selected several similar problems for you: 5324 5323 5322 5321 5320
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test output one line contains a integer,denoting the maximum size of Bobo Set.
[align=left]Sample Input[/align]
7
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7
[align=left]Sample Output[/align]
5
[align=left]Source[/align]
2015 Multi-University Training Contest 3
[align=left]Recommend[/align]
wange2014 | We have carefully selected several similar problems for you: 5324 5323 5322 5321 5320
/* 大意:找到最长的上升序列(要求连在一起) DP思想 dp[u] += dp[v] 从最长的开始找上升 */ #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; int n; int b[500010]; vector<int> G[500010]; int dp[500010]; struct edge{ int num, id; }a[500010]; bool cmp(edge i, edge j) { return i.num < j.num; } int main() { int x, y; while(~scanf("%d", &n)){ for(int i = 1; i < n ; i++) G[i].clear(); for(int i = 1; i <= n ; i++){ scanf("%d", &a[i].num); a[i].id = i; } for(int i = 1; i <= n; i++) b[i] = a[i].num; sort(a + 1, a + n + 1,cmp); for(int i = 1; i < n ; i++){ scanf("%d%d", &x, &y); G[y].push_back(x); G[x].push_back(y); } int max1 = 1; memset(dp, 0, sizeof(dp)); for(int i = n ; i >= 1; i--){ int u = a[i].id; dp[u] = 1; for(int j = 0 ; j < G[u].size(); j++){ int v = G[u][j]; if(b[v] > b[u]) { dp[u] += dp[v]; // printf("%d\n", dp[u]); } } max1 = max(dp[u], max1); } printf("%d\n", max1); } return 0; }
相关文章推荐
- Atom飞行手册翻译: 2.1 Atom中的包
- Netty百万级推送服务设计要点
- Java NIO
- PB中字符编码转换
- Oracle 表id实现自增
- BIEE初次启动密码输入错误
- 【语言-C++】MD5加密算法 32bit 16bit 源码
- Unity5中Inspector界面上的AssetBundle值设定问题
- Https流程(简单认识)
- HDUOJ 1896(优先队列)
- 写在Demo战斗系统之前,先用原型工具做套UI第二篇-人物选择界面制作
- css文字样式
- Linux中用mdadm管理软件raid
- Power Network
- C++_子类与基类的构造函数问题
- poj 2151 概率dp
- 新手小问题总结
- Power Network 分类: POJ 2015-07-29 13:55 3人阅读 评论(0) 收藏
- IOS GCD queue group
- 黑马程序员--反射Reflect