pid=5325">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 (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
/**
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;
}

hdu5325 树的思维题的更多相关文章

  1. P5385 [Cnoi2019]须臾幻境(LCT+主席树,思维题)

    题目 P5385 [Cnoi2019]须臾幻境 做法 考虑一条边\((u,v)\)是否\([L,R]\)中的贡献:\([L,R]\)中第一条位于\(u,v\)链的边,则减少了一个联通块 实现:\(LC ...

  2. 洛谷T44252 线索_分治线段树_思维题

    分治线段树,其实就是将标记永久化,到最后再统一下传所有标记. 至于先后顺序,可以给每个节点开一个时间戳. 一般地,分治线段树用于离线,只查询一次答案的题目. 本题中,标记要被下传 222 次. Cod ...

  3. Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(树,思维题)

    题意: 给有 n 个点的树的 n-1 条边从 0 到 n-2 编号,使得任意两点路径中未出现的最小数最小的方案. 思路: 先给所有度为 1 的点所在边编号,之后其他点可以随意编排. #include ...

  4. BZOJ4401: 块的计数 思维题

    Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...

  5. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  6. UVA.679 Dropping Balls (二叉树 思维题)

    UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...

  7. Xor 思维题

    Xor 思维题 题目描述 小\(Q\)与小\(T\)正在玩一棵树.这棵树有\(n\)个节点,编号为 \(1\),\(2\) \(3...n\),由\(n-1\)条边连接,每个节点有一个权值\(w_i\ ...

  8. HDU 4578 Transformation --线段树,好题

    题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...

  9. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

随机推荐

  1. 牛客网暑期ACM多校训练营 记录

    所有牛客多校的做题记录请右转队伍wiki Name Date Rank 2018 Multi-University,Nowcoder Day 1 2018.7.19 16/338 5/10 https ...

  2. 洛谷2085最小函数值(minval) + 洛谷1631序列合并

    题目描述 有n个函数,分别为F1,F2,-,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个). ...

  3. Fatal error: Call to a member function read() on a non-object in

    是你的路径出问题了系统 > 系统基本参数 > 站点设置 里面的<站点根网址:和 网页主页链接:>系统 > 系统基本参数 > 核心设置 <DedeCMS安装目录 ...

  4. ylb: 触发器(Trigger)之Instead Of触发器 [注:没内容]

    ylbtech-SQL Server:SQL Server-触发器(Trigger)之Instead Of触发器 触发器(Trigger)之Instead Of触发器 [注:没内容]. ylb: 触发 ...

  5. 面向对象程序的设计原则--Head First 设计模式笔记

    一.找出应用中可能需要变化的地方,把它们独立出来,不要和那些不需要变化的代码混在一起. 把会变化的部分取出并“封装”起来,好让其他部分不会受到影响.这样,代码变化引起的不经意后果变少,系统变得更有弹性 ...

  6. VueJS组件之全局组件与局部组件

    全局组件 所有实例都能用全局组件. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  7. Snubber电路

    http://www.elecfans.com/yuanqijian/dianrongqi/20170601520736.html https://wenku.baidu.com/view/166f1 ...

  8. Lua学习八----------Lua运算符

    © 版权声明:本文为博主原创文章,转载请注明出处 1.Lua运算符: - 算术运算符:+(加法).-(减法).*(乘法)./(除法-取整).%(除法-取余).^(乘幂).-(负号) - 关系运算符:= ...

  9. spring学习六----------Bean的配置之Aware接口

    © 版权声明:本文为博主原创文章,转载请注明出处 Aware Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源 通过Aware接口,可以对S ...

  10. ios面试基础

    1.#import和#include的差别 @class? @class一般用于头文件里须要声明该类的某个实例变量的时候用到,在m文 件中还是须要使用#import 而#import比起#includ ...