Bob’s Race

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4123

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

HINT

题意

一个城镇有N个住户,N-1条路连接两个住户,保证N个住户联通,M次询问,给定N条边的信息,包括连
接的住户序号以及路的长度。然后是M次询问,每次询问Q,要求找到最长的连续序号,使得Max(dis[i]) - Min(dis[i]) ≤
Q(l≤i≤r),输出最大的r-l+1。dis[i]为从第i个住户出发,不重复走过路能移动的最远距离。

题解:

dis[i]一定是点到直径的某个点的距离,所以我们两次dfs求出直径,然后两次dfs求出距离就好了

至于第二问,对于每个询问,我们可以O(N)扫一遍就好了

代码

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std; vector<pair<int,int> > Q[];
int d[];
int n,m;
int left1,l_num;
int Right,r_num;
int dpMax[][], dpMin[][];
void dfs1(int x,int pre,int len,int type)
{
if(type == && l_num < len)
{
l_num = len;
left1 = x;
}
if(type == && r_num < len)
{
r_num = len;
Right = x;
}
for(int i=;i<Q[x].size();i++)
{
pair<int,int> K = Q[x][i];
if(K.first == pre)continue;
dfs1(K.first,x,len + K.second,type);
}
}
void dfs2(int x,int pre,int len)
{
d[x]=max(d[x],len);
for(int i=;i<Q[x].size();i++)
{
pair<int,int> K = Q[x][i];
if(K.first == pre)continue;
dfs2(K.first,x,len + K.second);
}
}
void rmq_init() {
for (int i = ; i <= n; i++)
dpMax[i][] = dpMin[i][] = d[i]; for (int k = ; (<<k) <= n; k++) {
for (int i = ; i + (<<k) - <= n; i++) {
dpMax[i][k] = max(dpMax[i][k-], dpMax[i+(<<(k-))][k-]);
dpMin[i][k] = min(dpMin[i][k-], dpMin[i+(<<(k-))][k-]);
}
}
} int rmq_query(int l, int r) {
int k = ;
while ((<<(k+)) <= r - l + ) k++;
return max(dpMax[l][k], dpMax[r-(<<k)+][k]) - min(dpMin[l][k], dpMin[r-(<<k)+][k]);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)break;
memset(d,,sizeof(d));
memset(dpMax,,sizeof(dpMax));
memset(dpMin,,sizeof(dpMin));
for(int i=;i<=n;i++)
Q[i].clear();
for(int i=;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Q[x].push_back(make_pair(y,z));
Q[y].push_back(make_pair(x,z));
}
left1 = l_num = Right = r_num = ; dfs1(,-,,);
dfs1(left1,-,,);
dfs2(left1,-,);
dfs2(Right,-,);
rmq_init();
while (m--) {
int ans = , mv = ;
int QQ;
scanf("%d", &QQ);
for (int i = ; i <= n; i++) {
while (mv <= i && rmq_query(mv , i) > QQ) mv++;
ans = max(ans, i - mv + );
}
printf("%d\n", ans);
} }
}

HDU 4123 Bob’s Race 树的直径 RMQ的更多相关文章

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

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

  2. HDU 4123 Bob’s Race 树的直径+单调队列

    题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...

  3. HDU 4123 Bob’s Race 树的直径+ST表

    Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...

  4. HDU 4123 Bob's Race:树的直径 + 单调队列 + st表

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...

  5. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  6. hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)

    C - Bob’s Race Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  7. HDU 4123 Bob’s Race(树形DP,rmq)

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

  8. HDU 4123 Bob’s Race(RMQ)

    题意是说给出一棵树,N(10^5)个顶点,以及每条边的权值,现在需要选择连续的K个点(顶点编号连续),可以被选出来的条件是: 若d[i]代表顶点i到树上其他点的距离的最大值,使得区间[a, b]的d值 ...

  9. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

随机推荐

  1. Banner 广告设计技巧及经验(转自UI中国)

    经常听到banner这个词,但是banner是什么意思呢?引用百度知道的解释:banner可以作为网站页面的横幅广告,也可以作为游行活动时用的旗帜,还可以是报纸杂志上的大标题.Banner主要体现中心 ...

  2. POJ 2136 Vertical Histogram

    题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...

  3. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...

  4. RMQ(dp)

    我一开始是不知道有这么个东西,但是由于最近在学习后缀数组,碰到一道题需要用到后缀数组+RMQ解决的所以不得不学习了. 原理:用A[1...n]表示一组数,dp[i][j]表示从A[i]到A[i+2^j ...

  5. 《C++ primer》--第三章

    习题3.2 什么是默认构造函数? 解答: 默认构造函数就是在没有显示提供初始化式时调用的构造函数.它由不带参数的构造函数,或者为所有形参提供默认实参的构造函数定义.如果定义某个类的变量时没有提供初始化 ...

  6. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (六) 自动化测试结构小节

    上一篇‘e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (五) 如何让窗体记录登录 ...

  7. DbContext运行时动态附加上一个dbset

    参考 Creating DbSet Properties Dynamically C# code? 1 DbSet<MyEntity> set = context.Set<MyEnt ...

  8. C# 中如何判断某个字符串是否为空的方法 分享了三个方法来判断字

    1. 三种常用的字符串判空串方法:Length法:bool isEmpty = (str.Length == 0);Empty法:bool isEmpty = (str == String.Empty ...

  9. cannot conform to protocol 的一点事

    学习UITableView过程中,回想视频打一遍代码,发现卡在了第一步.一直显示无法继承协议,而且还多了一个错误:definition conflicts with previous value.百度 ...

  10. Hello, Github Blog

    hello, I am using github to write a post, I am so exciting- 原文地址: http://vblog.vell001.ml/2014/03/08 ...