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. JS浮点计算问题

    问题 用js进行浮点数计算,结果可能会“超出预期”,大部分计算结果还是对的,但是我们可不想在计算这么严谨的事情上还有意外的惊喜.比如: 0.3 + 0.6 = 0.8999999999999999 0 ...

  2. Yarn架构

    jobtracker存在单点故障问题 jobtracker只支持mapreduce,计算框架不具有可扩展性 jobtracker是性能瓶颈 yarn可以整合不同的计算框架,提高资源利用率 yarn的基 ...

  3. mysql忘记密码怎么办?(转)

    linux下mysql忘记密码处理方法: 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码 ...

  4. Google Code Jam 2014 总结

    第一次参加ACM竞赛,对自己取得的成绩还满意. Round1A: Rank: 2446 Score: 9 (没有进前1000名,只能拼下次了) Round1B: Rank: 944 Score: 42 ...

  5. css-1,css的三种引入方式 基本选择器

    <!-- (1)CSS 层叠样式表 作用:修饰网页结构 (2)css的三种引入方式 权重: 优先级高 权重大 谁在页面谁的权重大 - 行内样式 注意:行内样式的优先级是最高的 - 内接样式 - ...

  6. ios 常见问题解决 以及小技巧

    1.使用cocoaPods引用第三方类库,报错:file not found   . 解决方案:设置 Project->Info->Configurations之后  clear ,然后再 ...

  7. 对Spring Ioc 以及DI的精彩理解

    转:http://blog.csdn.net/cyjs1988/article/details/50352916 学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注 ...

  8. 让boost.variant支持lambda表达式访问

    前言 之前写个过一篇博客叫<浅谈boost.variant的几种访问方式>,里面讲到了可以通过访问者方式来获取variant的值,但是在重载函数operator()里面只能够获取varia ...

  9. [入坑系列] Mybatis 中$与#的区别

    1.理解 1 #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 sel ...

  10. Django学习笔记之Django QuerySet的方法

    一般情况下,我们在写Django项目需要操作QuerySet时一些常用的方法已经满足我们日常大多数需求,比如get.filter.exclude.delete神马的感觉就已经无所不能了,但随着项目但业 ...