您的位置:首页 > 其它

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

/*
大意:找到最长的上升序列(要求连在一起)
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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: