hdu 4123 树形DP+RMQ

时间:2022-09-21 10:05:01

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

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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
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[u])
{
swap(maxn[u],smaxn[u]);
swap(maxid[u],smaxid[u]);
}
}
}
} void dfs2(int u,int pre)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(pre==v)continue;
if(maxid[u]==v)
{
if(smaxn[u]+edge[i].w>smaxn[v])
{
smaxn[v]=smaxn[u]+edge[i].w;
smaxid[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxid[v],smaxid[v]);
}
}
}
else
{
if(maxn[u]+edge[i].w>smaxn[v])
{
smaxn[v]=maxn[u]+edge[i].w;
smaxid[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxid[v],smaxid[v]);
}
}
}
dfs2(v,u);
}
} int a[N],n,m;
int dp1[N][30];
int dp2[N][30]; void RMQ_init(int n)
{
for(int i=1;i<=n;i++)
{
dp1[i][0]=a[i];
dp2[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)///白书上的模板,次行稍作修改。否则dp数组要扩大一倍防止RE
{
dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
}
}
} int rmq(int x,int y)
{
int k=0;
while((1<<(k+1))<=y-x+1)k++;
return max(dp1[x][k],dp1[y-(1<<k)+1][k])-min(dp2[x][k],dp2[y-(1<<k)+1][k]);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)break;
init();
for(int i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dfs1(1,-1);
dfs2(1,-1);
for(int i=1;i<=n;i++)
{
a[i]=maxn[i];
}
RMQ_init(n);
while(m--)
{
int Q;
scanf("%d",&Q);
int ans=0;
int id=1;
for(int i=1;i<=n;i++)
{
while(id<=i&&rmq(id,i)>Q)id++;
ans=max(ans,i-id+1);
}
printf("%d\n",ans);
}
}
return 0;
}

hdu 4123 树形DP+RMQ的更多相关文章

  1. hdu 4123 树形DP&plus;单调队列

    http://acm.hust.edu.cn/vjudge/problem/25790 这题基本同poj 3162 要注意mx,mx2,vx,vx2每次都要初始化 #include <iostr ...

  2. 树形DP&plus;RMQ&plus;尺取法 hdu4123

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 参考博客:两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race ...

  3. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  4. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. HDU 2196树形DP&lpar;2个方向&rpar;

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  6. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  7. codevs 1380&sol;HDU 1520 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...

  8. HDU 5834 &lbrack;树形dp&rsqb;

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

  9. hdu 4267 树形DP

    思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...

随机推荐

  1. 给Hi3518e的Uboot添加UDP广播收发功能

    基于个人兴趣,决定实现一个和方案公司提供的uboot收发广播的功能.记录笔记如下. SDK版本:Hi3518E_V100R001C01SPC081 1. 由于我手头的板子的Phy是RMII模式,因此先 ...

  2. 关于KB905474正版验证补丁破解办法 KB905474是个微软操作系统正版&sol;盗版监测间谍软件。更新安装后,右下角有个提示说&OpenCurlyDoubleQuote;系统监测到你的操作系统是盗版”。 如果没有安装的: 在系统提示更新的时候注意看一下,如果包含有&OpenCurlyDoubleQuote;更新KB905474”就去掉&OpenCurlyDoubleQuote;更新KB905474”方框前的勾,点击关闭(注意如果没有去掉那个勾得话,会找不到&OpenCurlyDoubleQuote;关闭”,而是&OpenCurlyDoubleQuote;确定”),在不在提示我该消息前打勾。 如果已经安装

    关于KB905474正版验证补丁破解办法 KB905474是个微软操作系统正版/盗版监测间谍软件.更新安装后,右下角有个提示说“系统监测到你的操作系统是盗版”. 如果没有安装的: 在系统提示更新的时候 ...

  3. &lbrack;HDU2089&rsqb;不要62

    [HDU2089]不要62 试题描述 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就 ...

  4. 用友U8账套的建立

      第1步点击开始菜单进入系统管理模块   第2步点击系统菜单下的注册   第3步弹出登录系统对话框,操作员输入admin点确定   第4步点击权限菜单下的用户   第5步进入用户管理窗口,点击工具栏 ...

  5. ASP&period;NET对路径&quot&semi;xxxxx&quot&semi;的访问被拒绝的解决方法小结

    异常详细信息: System.UnauthorizedAccessException: 对路径“D:/temp1/MyTest.txt”的访问被拒绝     在windows 2003下,在运行web ...

  6. 【linux基础】ubuntu系统NVIDIA驱动安装

    在安装GPU环境下的软件工具,特别是CUDA/CUDNN等,一定要先把GPU环境搭建好. NVIDIA驱动安装会遇到各种问题,真希望黄教主可以将各个工具如何安装使用讲解的更加细致.清楚一些,有时候按照 ...

  7. html image 圖像路徑

    src可以指定image路徑: alt可以設置替代的文本:當瀏覽器沒有辦法加載到圖片的時候,就會顯示替換的文本,提示什麼圖片未加載. width和heigt可以設置圖片的大小,從而對圖片進行縮放. h ...

  8. ES6 主要的新特性

    本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony( ...

  9. D11——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D11 20180908内容纲要: 1.RabbitMQ消息队列 (1)RabbitMQ安装 (2)Rabbits示例 模式一:fanout 模式二:direct ...

  10. SDOI 2017 Round1 解题报告

    Day 1 T1 数字表格 题目大意 · 求\(\prod\limits_{i=1}^n\prod\limits_{j=1}^mFibonacci(\gcd(i,j))\),\(T\leq1000\) ...