Bob’s Race

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3034    Accepted Submission(s): 991

Problem Description
Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses, and all houses are connected together. To make the race more interesting,
he requires that every participant must start from a different house and run AS FAR AS POSSIBLE without passing a road more than once. The distance difference between the one who runs the longest distance and the one who runs the shortest distance is called
“race difference” by Bob. Bob does not want the “race difference”to be more than Q. The houses are numbered from 1 to N. Bob wants that the No. of all starting house must be consecutive. He is now asking you for help. He wants to know the maximum number of
starting houses he can choose, by other words, the maximum number of people who can take part in his race.
 
Input
There are several test cases.
The first line of each test case contains two integers N and M. N is the number of houses, M is the number of queries.
The following N-1 lines, each contains three integers, x, y and z, indicating that there is a road of length z connecting house x and house y.
The following M lines are the queries. Each line contains an integer Q, asking that at most how many people can take part in Bob’s race according to the above mentioned rules and under the condition that the“race difference”is no more than Q.

The input ends with N = 0 and M = 0.

(N<=50000 M<=500 1<=x,y<=N 0<=z<=5000 Q<=10000000)

 
Output
For each test case, you should output the answer in a line for each query.
 
Sample Input
5 5
1 2 3
2 3 4
4 5 3
3 4 2
1
2
3
4
5
0 0
 
Sample Output
1
3
3
3
5

树的最长路:

用搜索的方法求某个点的最远的点的距离了,就是先对任意一个点求距离其最远的顶点,最后可以得到一条树的直径的两个端点,以这两个端点开始去遍历整棵树,两个端点到每个点的距离较大值就会是这个点在树上能够走的最远距离

/*
hdu4123
给你n个点,被n-1条边连着,求出以他们每个点为起点的最长路(不可重复走),
然后是m个查询,找出它们的最长连续串max-min<q
树的最长路 + RMQ
hhh-2016-01-31 03:04:55
*/ #include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <Map>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std; const int maxn = 50005; ll dp1[maxn][20];
ll dp2[maxn][20];
int mm[maxn+5];
ll sum[maxn];
int tot;
int head[maxn]; ll min(ll a,ll b)
{
return a < b ? a:b;
} ll max(ll a,ll b)
{
return a > b ? a:b;
} struct node
{
int to,next;
ll w;
} edge[maxn*2]; void addedge(int u,int v,int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} void iniRMQ(int n,ll c[])
{
mm[0] = -1;
for(int i = 1; i <= n; i++)
{
mm[i] = ((i&(i-1)) == 0)? mm[i-1]+1:mm[i-1];
dp1[i][0]=dp2[i][0]= c[i];
}
for(int j = 1; j <= mm[n]; j++)
{
for(int i = 1; i+(1<<j)-1 <= n; i++)
{
dp1[i][j] = min(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
dp2[i][j] = max(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
}
}
} ll RMQ(int x,int y)
{
int k = mm[y-x+1];
return (ll)max(dp2[x][k],dp2[y-(1<<k)+1][k])-(ll)min(dp1[x][k],dp1[y-(1<<k)+1][k]);
}
int id;
ll tall; void dfs(int u,int pre,ll cnt) //先找出最远点
{
if(cnt >= tall)
{
id = u;
tall = cnt;
}
for(int i = head[u]; ~i; i = edge[i].next)
{
if(edge[i].to == pre) continue;
dfs(edge[i].to,u,edge[i].w+cnt);
}
} void cal(int u,int pre,ll cnt)
{
for(int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if(v == pre)
continue;
sum[v] = max(sum[v],cnt+edge[i].w);
cal(v,u,cnt+edge[i].w);
}
} int main()
{
int m,n,k;
while(scanf("%d%d",&n,&m) && n && m)
{
int u,v,val;
tot = 0;
memset(head,-1,sizeof(head));
memset(sum,0,sizeof(sum));
for(int i = 1; i < n; i++)
{
scanf("%d%d%d",&u,&v,&val);
addedge(u,v,val);
addedge(v,u,val);
}
tall = 0;
dfs(1,0,0);
cal(id,0,0);
int t = 1;
for(int i = 1;i <= n;i++)
if(sum[i] > sum[t]) t = i;
// printf("%d %d\n",id,t);
cal(t,0,0);
// for(int i = 1;i <= n;i++)
// printf("%d ",sum[i]);
// cout << endl;
iniRMQ(n,sum);
while(m--)
{
scanf("%d",&k);
int ans = 0;
int td = 1;
for(int i = 1;i <= n;i++)
{
while(td <= i && RMQ(td,i) > k) td++;
ans = max(ans,i-td+1);
}
printf("%d\n",ans);
}
}
return 0;
}

  

hdu 4123 树的最长路+RMQ的更多相关文章

  1. HDU 2196 Computer (树上最长路)【树形DP】

    <题目链接> 题目大意: 输出树上每个点到其它点的最大距离. 解题分析: 下面的做法是将树看成有向图的做法,计算最长路需要考虑几种情况. dp[i][0] : 表示以i为根的子树中的结点与 ...

  2. 有趣的线段树模板合集(线段树,最短/长路,单调栈,线段树合并,线段树分裂,树上差分,Tarjan-LCA,势能线段树,李超线段树)

    线段树分裂 以某个键值为中点将线段树分裂成左右两部分,应该类似Treap的分裂吧(我菜不会Treap).一般应用于区间排序. 方法很简单,就是把分裂之后的两棵树的重复的\(\log\)个节点新建出来, ...

  3. hihoCoder 1050 树中的最长路 最详细的解题报告

    题目来源:树中的最长路 解题思路:枚举每一个点作为转折点t,求出以t为根节点的子树中的‘最长路’以及与‘最长路’不重合的‘次长路’,用这两条路的长度之和去更新答案,最终的答案就是这棵树的最长路长度.只 ...

  4. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  5. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  6. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  7. hiho #1050 : 树中的最长路 树的直径

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  8. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  9. HDU 4607 Park Visit (树的最长链)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. js正则表达语法

    /* *通过量词可以设置一个内容出现的次数 *量词只对它前边的一个内容起作用.所以在作用多个时需要用小括号()来向计算机说明这是一个整体. *-{n}代表正好出现n次. *-{m,n}出现了m-n次. ...

  2. vuex - 项目结构目录及一些简单配置

    首先先正经的来一段官网的"忠告": vuex需要遵守的规则: 一.应用层级的状态应该集中到单个 store 对象中. 二.提交 mutation 是更改状态的唯一方法,并且这个过程 ...

  3. 老帖收藏,留供参考:SpringMvc2.5+Mybatis3.2.7

    一.项目背景 SpringMvc+Mybatis 数据库连接池是阿里巴巴的druid.日志框架式logback 二.配置文件 1.SpringMvc-servlet.xml <?xml vers ...

  4. ASP.NET MVC5 Forms登陆+权限控制(控制到Action)

    一.Forms认证流程 请先参考如下网址: http://www.cnblogs.com/fish-li/archive/2012/04/15/2450571.html 本文主要介绍使用自定义的身份认 ...

  5. jQuery ajax方法success()中后台传来的四种数据类型

    1.后台返回一个页面 js代码 /**(1)用$("#content-wrapper").html(data);显示页面*/ $.ajax({ async : false, cac ...

  6. Properties文件中文属性读取是乱码问题

    项目当中遇到了需要从Properties文件中读取配置属性的需求,本来是存储的中文转码后的属性,但是考虑到后期更改问题就变成java代码中进行转码,代码如下: Properties pros = ne ...

  7. maven项目添加db2的jar包

    安装完DB2后,SQLLIB文件夹下的java目录下有对应的jar包,我的SQLLIB文件夹位置在 D:\Program Files\IBM\SQLLIB\java 处. 此目录直接添加到CLASSP ...

  8. 在一个没有设置宽高的容器中,为什么设置position:absolute后就可以全屏显示了?

    此场景适用于移动端百分比布局,背景全屏显示. 在一个没有设置宽高的容器中设置背景,想要背景全屏显示,设置bcakground-size:100%;后还需设置position:absolut; 原因: ...

  9. C#之序列化对象(二进制方式序列化对象)

    应用程序有时需要以对象的形式在磁盘上存储数据,FrameWork有两个可用的实现方式: 一:System.Runtime.Serialization.Formatters.Binarry这个名称空间包 ...

  10. ASP.NET Core + Docker + Jenkins + gogs + CentOS 从零开始搭建持续集成

    为什么不用gitlab? 没有采用gitlab,因为gitlab比较吃配置,至少得2核4G的配置.采用go语言开发的gogs来代替,搭建方便(不到10分钟就能安装完成),资源消耗低,功能也比较强大,也 ...