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. CodeForces - 16E Fish

    Discription n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and ...

  2. Ubuntu 16.04下IntelliJ IDEA菜单显示乱码的问题解决

    说明:这个问题一般是Ubuntu安装时默认使用了中文,而IDEA默认是以系统默认字体为主,所以就会出现乱码:要解决这种问题,就是在IDEA启动时强制设置为英文. 解决方式: 在idea.vmoptio ...

  3. 怎样去主动拿一个锁并占有?synchronized关键字即可

    怎样主动去拿一个?synchronized关键字即可 怎样去释放一个锁呢?要求锁对象主动释放,打乱占有当前锁的线程即可

  4. mysql日常运维与参数调优

    日常运维 DBA运维工作 日常 导数据,数据修改,表结构变更 加权限,问题处理 其它 数据库选型部署,设计,监控,备份,优化等 日常运维工作: 导数据及注意事项 数据修改及注意事项 表结构变更及注意事 ...

  5. Git的提交忽略文件

    .gitingore文件内容如下 /target//.settings//.classpath/.project/logs/

  6. VIM正则表达式查找替换

      0. 一些需要注意的不同 VIM中的正则表达式和其他的有点不一样 (1) 有些符号要用\转义,比如\+表示重复一次或以上,其他的还有一些,:h pattern查看(2) 非贪婪匹配用\{-}, 如 ...

  7. NYOJ92 图像实用区域 【BFS】

    碰到了一个曾经从未见过的奇怪问题:先上截图: 执行号 用户 题目 结果 时间 内存 语言 提交时间 895360 userid=%E9%95%BF%E6%9C%A8" style=" ...

  8. 为电脑添加u盘写保护

    需求:解决在公共打印PC机上u盘病毒的传染,设置后该PC机将不能对u盘文件进行写操作 修改注册表,在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro ...

  9. vim diff 的使用

    vimdiff 是vim的比较工具可以对两个文件进行差异比较和快速合并 1. 使用vimdiff 比较两个文件 方式一 vimdiff  file_left  file_right   或者  vim ...

  10. 小白学习python之路(二):安装开发工具

    引言 上一章我们安装配置了python3.7,这一章我们安装python的开发工具,我用的pycharm2019 安装 工具连接:https://u20538204.ctfile.com/fs/205 ...