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. pycharm使用技巧。(mac版本)

    一.pycharm使用中的一些快捷键 1.cmd  + b 跳转到声明处(cmd加鼠标) 2.option + c 复制光标当前行,剪切同理 3.option + v 粘贴复制的行 4.option ...

  2. kettle配置命名参数

    bat 调度文件如下 cd D:/Program Files/kettle700/data-integrationKitchen.bat /rep repository /dir /TEST /job ...

  3. Python(^^^^^小技巧^^^^^——不定期更新)

    偶然想到的小技巧 ''' 交互中对传入函数的参数的数目进行检测 ''' def func(a,b,c): print(a,b,c) s=input(">>>>:&qu ...

  4. AviMemDc: a C++ class

    AviMemDc: a C++ class        This class is used in the Avi Examples.The header fileAviMemDC.h /*    ...

  5. JS:parseInt("08")或parseInt("09")转换返回0的原因

    一.parseInt用法 parseInt(s); parseInt(s,radix) 二.第一个方式不再多说,第二个方式,radix是s所基于的进制.范围为2-36(不在此范围函数将返回NaN). ...

  6. JVM内存—堆(heap)栈(stack)方法区(method) (转)

    JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...

  7. centos ssh免密码秘钥登录

    假设从A主机ssh登录B主机,用秘钥代替密码,步骤如下: 1.在A主机上执行:ssh-keygen -t  rsa 一切默认,不用输入密码,生成两个文件: /root/.ssh/id_rsa /roo ...

  8. JS正则表达式从入门到入土(2)—— 元字符和字符类

    元字符和字符类 元字符 正则表达式由两种基本字符类型组成: 1.原义(正常)文本字符:代表本身含义的字符,如:a.b.c.1.2.3等. 2.元字符:元字符是在正则表达式中有特殊含义的非字母字符,如\ ...

  9. Printf的缓冲机制

    转:https://blog.csdn.net/qq_25424545/article/details/78772959 今天用fork()写程序时候,突然发现自己对Printf的缓冲机制还是有些不够 ...

  10. Git笔记之初识vi编辑器

    1.vi编辑器 如同Windows下的记事本,vi编辑器是Linux下的标配,通过它我们可以创建.编辑文件.它是一个随系统一起安装的文本编辑软件. vi编辑器提供了3种模式,分别是命令模式.插入模式. ...