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. 20165324 学习基础与C语言学习心得

    20165324 技能学习心得与C语言学习 一.读后感及技能学习 做中学读后感 我认为给学生具体的, 能实践的, 能马上看到因果关系的教材和练习, 是激发学生兴趣, 好奇心, 求知欲的好方法. 我就是 ...

  2. 这样才能使本地Mysql服务允许被外部主机连接(两步)

    网上的N多方法都不全面,只有下面的第一步或第二步是不行的,必须同时执行下面两步操作: 修改mysql.user表 以root或debian-sys-maint身份登录mysql $ mysql -u ...

  3. java反射之获取类的基本信息(一)

    一.反射原理. Java 反射机制.通俗来讲呢,就是在运行状态中,我们可以根据“类的部分已经的信息”来还原“类的全部的信息”.这里“类的部分已经的信息”,可以是“类名”或“类的对象”等信息.“类的全部 ...

  4. Spring入门学习推荐

    该作者的 spring 博客很好,作为学习用 简介 https://blog.csdn.net/column/details/15088.html?&page=1 https://blog.c ...

  5. Zookeeper那些事

    一. 什么是Zookeeper Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoop 的分布式协调服务 它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务, ...

  6. 2016-2017 National Taiwan University World Final Team Selection Contest A - Hacker Cups and Balls

    题目: Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figur ...

  7. 如何释放linux cache占用的内存

    [root@prd-fygl-app-01 ~]# free -m             total       used       free     shared    buffers     ...

  8. redis 简单命令操作

    一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命令都具有一个共同点,即所有的操作都是针对与K ...

  9. Ubuntu16.04中用et对jmeter生成的数据统计成图表

    在Ubuntu系统中,用ctrl+Alt+t 打开终端: 输入et,即打开wps: 整理需要形成图表的数据,如: 用excel生成图表,如下: 表得出的性能图表,方法: 1.工具栏中选择插入——二维折 ...

  10. 在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示

    在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示) 1.使用npm安装依赖 npm install --save codemirror; 2.在页面中放入如下代码 ...