博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4123 树形DP+RMQ
阅读量:6069 次
发布时间:2019-06-20

本文共 2869 字,大约阅读时间需要 9 分钟。

pid=4123

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
/**hdu 4123 树形DP+RMQ题目大意:给定一棵树,每个点都从当前位置走到距离最远的位置。1~n的连续区间中最大而且走的最远距离差值不超过Q的区间右多大解题思路:两遍dfs求出在树中到当前点的最长距离:dfs1求出以当前节点为根节点的子树中到该节点的最长距离和次长距离,dfs2将上一步求出的          最长距离和经过根节点的最长距离比較取最大。利用RMQ查询寻找最大区间*/#include 
#include
#include
#include
using namespace std;const int N=50050;int head[N],ip;struct note{ int v,w,next;}edge[N*2];void init(){ memset(head,-1,sizeof(head)); ip=0;}void addedge(int u,int v,int w){ edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;}int maxn[N],smaxn[N],maxid[N],smaxid[N];void dfs1(int u,int pre){ maxn[u]=smaxn[u]=maxid[u]=smaxid[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(pre==v)continue; dfs1(v,u); if(maxn[v]+edge[i].w>smaxn[u]) { smaxid[u]=v; smaxn[u]=maxn[v]+edge[i].w; if(maxn[u]
smaxn[v]) { smaxn[v]=smaxn[u]+edge[i].w; smaxid[v]=u; if(maxn[v]
smaxn[v]) { smaxn[v]=maxn[u]+edge[i].w; smaxid[v]=u; if(maxn[v]
Q)id++; ans=max(ans,i-id+1); } printf("%d\n",ans); } } return 0;}

转载地址:http://acfgx.baihongyu.com/

你可能感兴趣的文章
android实用测试方法之Monkey与MonkeyRunner
查看>>
「翻译」逐步替换Sass
查看>>
H5实现全屏与F11全屏
查看>>
处理excel表的列
查看>>
枸杞子也能控制脂肪肝
查看>>
Excuse me?这个前端面试在搞事!
查看>>
C#数据采集类
查看>>
XShell提示Connection closed by foreign host的问题 和 路由器分配IP的规则
查看>>
Win8快捷键
查看>>
83. Remove Duplicates from Sorted List - Easy
查看>>
栈讲解——整理
查看>>
【原】MAC显示隐藏文件夹命令
查看>>
每天一道LeetCode--374. Guess Number Higher or Lower
查看>>
quicksort
查看>>
"Ray, Pass me the dishes!" UVALive - 3938 (线段树)
查看>>
有关于key值
查看>>
MyEclipse10中导入的jquery文件报错(出现红叉叉,提示语法错误)
查看>>
cursor:not-allowed
查看>>
检验函数运行时间
查看>>
【转】Objective-C学习笔记八:类的定义二
查看>>