HDU5325——DP+搜索——Crazy Bobo

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 (1n500000). Then following a line contains n integers w1,w2,...,wn (1wi109,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 (1ai,bin).
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:  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;
}

  

posted @ 2015-07-29 13:57  Painting、时光  阅读(121)  评论(0编辑  收藏  举报