Bob’s Race

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

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
         首先我们应该先用树形dp计算出从每个节点作为起点的最长路长度,将结果保存fm[i]中。对于每次询问需要知道区间最大/小值,这里用RMQ预处理后能在O(1)内计算出来。对于求最长的长度,一开始想的是二分,结果T了,后来换成尺取在O(N)内就能得到,少了个log就过了。
      ps:RMQ至今只会套板子= =

 #include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define sf(a) scanf("%d",&a)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
const int MAXN=;
int N,M;
int fm[MAXN],sm[MAXN];
int fid[MAXN],sid[MAXN];
int f1[MAXN][],f2[MAXN][];
vector<pii>g[MAXN];
void dfs1(int u,int fa)
{
fm[u]=sm[u]=;
fid[u]=sid[u]=;
for(int i=;i<g[u].size();++i){
int v=g[u][i].first;
int w=g[u][i].second;
if(v==fa) continue;
dfs1(v,u);
if(w+fm[v]>sm[u]){
sm[u]=w+fm[v];
sid[u]=v;
if(sm[u]>fm[u]){
swap(fm[u],sm[u]);
swap(fid[u],sid[u]);
}
}
}
}
void dfs2(int u,int fa,int w)
{
if(w+fm[fa]>sm[u]&&fid[fa]!=u){
sm[u]=w+fm[fa];
sid[u]=fa;
if(sm[u]>fm[u]){
swap(fm[u],sm[u]);
swap(fid[u],sid[u]);
}
}
if(w+sm[fa]>sm[u]&&sid[fa]!=u){
sm[u]=w+sm[fa];
sid[u]=fa;
if(sm[u]>fm[u]){
swap(fm[u],sm[u]);
swap(fid[u],sid[u]);
}
}
for(int i=;i<g[u].size();++i){
int v=g[u][i].first;
int ww=g[u][i].second;
if(v==fa) continue;
dfs2(v,u,ww);
}
}
void initRmq()
{
for(int i=;i<N;++i){
f1[i][]=f2[i][]=fm[i+];
}
for(int j=;(<<j)<=N;j++){
for(int i=;i+(<<j)-<N;i++){
f1[i][j]=min(f1[i][j-],f1[i+(<<(j-))][j-]);
f2[i][j]=max(f2[i][j-],f2[i+(<<(j-))][j-]);
}
}
}
bool rmq(int L,int R,int Q)
{
L--;
R--;
int k=;
while((<<(k+))<=R-L+) k++;
int minn=min(f1[L][k],f1[R-(<<k)+][k]);
int maxn=max(f2[L][k],f2[R-(<<k)+][k]);
return maxn-minn<=Q;
}
int main()
{
int i,j,k;
int u,v,w,Q;
while(cin>>N>>M&&(N||M)){
for(i=;i<N;++i){
sfff(u,v,w);
g[u].push_back(mp(v,w));
g[v].push_back(mp(u,w));
}
dfs1(,);
dfs2(,,);
initRmq();
while(M--){
sf(Q);
int l=,r=,ans=; //二分会T,这里用尺取能达到O(N)
int n=N;
for(l=;l<=n;++l){
while(r<=n&&rmq(l,r,Q)) r++;
ans=max(ans,r-l);
}
printf("%d\n",ans);
}
for(i=;i<=N;++i)g[i].clear();
}
return ;
}
Source

HDU-4123-树形dp+rmq+尺取的更多相关文章

  1. hdu 4123 树形DP+RMQ

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

  2. hdu4123-Bob’s Race(树形dp+rmq+尺取)

    题意:Bob想要开一个运动会,有n个房子和n-1条路(一棵树),Bob希望每个人都从不同的房子开始跑,要求跑的尽可能远,而且每条路只能走最多一次.Bob希望所有人跑的距离的极差不大于q,如果起点的编号 ...

  3. 树形DP+RMQ+尺取法 hdu4123

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

  4. hdu 4123 树形DP+单调队列

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

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

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

  6. hdu 4123--Bob’s Race(树形DP+RMQ)

    题目链接 Problem Description Bob wants to hold a race to encourage people to do sports. He has got troub ...

  7. HDU 1520 树形dp裸题

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

  8. HDU 1561 树形DP入门

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

  9. HDU 5834 [树形dp]

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

随机推荐

  1. windows批处理初学贴出一些命令

    在cmd窗口中复制时,右键选标记,然后再选择此时选择区域就变白了.然后要么直接拖到要粘贴的地方,要么直接按回车存到剪贴板里. 1.循环导入文件夹下面的文件到数据库中 cd /d D:/Program ...

  2. string与CString对比

    string是标准C++库中的字符串类,CString是在Windows开发环境下常用的字符串类,CString目前已从MFC中分离出来可以单独使用,只需包含atlstr.h即可. 相比string, ...

  3. Spark集群 Python Package管理

    具体问题: 不同的数据分析人员/开发团队需要不同版本的Python版本执行PySpark. 同一Python版本下,需要安装多个Python库,甚至需要不同版本的库. 针对问题2的一个解决办法是将Py ...

  4. yarn nodes label (yarn 划分子集群)

    yarn node labels 特性给节点打标签可以把特性类似的节点分成一组,这样可以指定特定的应用执行在特定的机器群上.现在我们只支持节点划分,1.一个节点仅能有一个节点划分,即一个节点只能打一个 ...

  5. CSS清除浮动大全的8种方法

    清除浮动是每一个 web前台设计师必须掌握的机能.css清除浮动大全,共8种方法. 浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的 ...

  6. SQL学习笔记之SQL中INNER、LEFT、RIGHT JOIN的区别和用法详解

    0x00 建表准备 相信很多人在刚开始使用数据库的INNER JOIN.LEFT JOIN和RIGHT JOIN时,都不太能明确区分和正确使用这三种JOIN操作,本文通过一个简单的例子通俗易懂的讲解这 ...

  7. Kafka学习之(三)Centos下给PHP开启Kafka扩展(rdkafka)

    Centos版本:Centos6.4,PHP版本:PHP7. 在上一篇文章中使用IP为192.168.9.154的机器安装并开启了Kafka进行了简单测试,充当了Kafka服务器. 本篇文章新开启一台 ...

  8. git的应用

    对git的应用 (终于第一次用会git) 根据胡东晖同学的博客(使用git推送代码到开源中国以及IDEA环境下使用git)与热心指导,自己跟着做了,虽然途中出了很多很多问题,但是好在最后还是成功了!! ...

  9. Jquery15 插件

    学习要点: 1.插件概述 2.验证插件 3.自动完成插件 4.自定义插件 插件(Plugin)也成为 jQuery 扩展(Extension),是一种遵循一定规范的应用程序接口编写出来的程序.目前 j ...

  10. 重新想,重新看——CSS3变形,过渡与动画③

    这一篇主要谈谈CSS3的过渡属性. 过渡属性被设计的十分通俗易懂,属性写法为transition,有四个子属性: <transition-property> 表示需要过渡的属性[必须](本 ...