hdu5325 树的思维题

http://acm.hdu.edu.cn/showproblem.php?

pid=5325

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 ( 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
/**
hdu5325 树的思维题
题目大意:给定一颗树。每一个点都有一个权值。求出一棵子树。权值递增排序。要求相邻权值两个点的路径上的点的权值都要比这两个权值小
解题思路:对于树上的每条边,对于Wu<Wv。连一条u到v的边,从每一个点開始bfs。找出能遍历最多的点就是答案
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int maxn=1000505;
int head[maxn],ip;
void  init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

struct note
{
    int v,next;
}edge[maxn];

void addedge(int u,int v)
{
    edge[ip].v=v,edge[ip].next=head[u];head[u]=ip++;
}

pair <int ,int > p[maxn];
int w[maxn],dp[maxn],n;

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&w[i]);
            p[i]=make_pair(w[i],i);
        }
        sort(p+1,p+n+1);
        init();
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        int ans=0;
        for(int i=n;i>=1;i--)
        {
            int u=p[i].second;
            dp[u]=1;
            for(int j=head[u];j!=-1;j=edge[j].next)
            {
                int v=edge[j].v;
                if(w[u]<w[v])
                {
                    dp[u]+=dp[v];
                }
            }
            ans=max(ans,dp[u]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


转载于:https://www.cnblogs.com/jzssuanfa/p/7270284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值