C - Bob’s Race

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
 
题意: 给一颗无向有权树,选择一些连续的点作为起点,每个起点u有一个人跑步要尽可能的跑得远,用ai表示第i个点作为起点能跑得最远的距离,起点选择l...r这些点,那么对每个询问求一对l和r,满足max(al...ar) - min(al...ar) <= Q, 要求l和r的差值尽量大,输出这个最大差值
思路: 首先dfs+dp求每个点u能跑的最远距离,然后对每个询问用一个双指针扫一遍过去,复杂度o(n)
 
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = ; struct _edge{
int to,w,next;
};
_edge edge[N<<];
int ecnt,head[N];
inline void addedge(int u,int v,int w)
{
edge[ecnt].to = v;
edge[ecnt].w = w;
edge[ecnt].next = head[u];
head[u]=ecnt++;
} int n,m,a[N];
int dp[N][],id[N][]; void dfs1(int u,int fa)
{
dp[u][]=dp[u][]=;
for(int e=head[u];e!=-;e=edge[e].next)
{
int &v = edge[e].to;
if(v==fa) continue;
dfs1(v,u);
int t1 = dp[v][] + edge[e].w, t2 = v;
if(t1>=dp[u][])
{
swap(t1,dp[u][]);
swap(t2,id[u][]);
}
if(t1>=dp[u][])
{
swap(t1,dp[u][]);
swap(t2,id[u][]);
}
}
} void dfs2(int u,int fa,int up)
{
a[u]=max(dp[u][],up);
for(int e=head[u];e!=-;e=edge[e].next)
{
int &v = edge[e].to;
if(v==fa) continue;
int t;
if(v==id[u][])
t = max(dp[u][],up);
else
t = max(dp[u][],up);
dfs2(v,u,t+edge[e].w);
}
} int dmx[N][],dmn[N][];
void RMQ_init()
{
for(int i=;i<=n;i++) dmn[i][]=dmx[i][]=a[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dmn[i][j]=min(dmn[i][j-],dmn[i+(<<(j-))][j-]),
dmx[i][j]=max(dmx[i][j-],dmx[i+(<<(j-))][j-]);
}
void query(int l,int r,int &mn,int &mx)
{
int k = ;
while((<<(k+))<=r-l+) k++;
mn = min(dmn[l][k],dmn[r-(<<k)+][k]);
mx = max(dmx[l][k],dmx[r-(<<k)+][k]);
} void run()
{
int u,v,w;
ecnt=;
memset(head,-,sizeof(head));
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
memset(dp,-,sizeof(dp));
memset(id,-,sizeof(id));
dfs1(,-);
dfs2(,-,);
// for(int i=1;i<=n;i++)
// cout<<a[i]<<' ';
// cout<<endl; RMQ_init(); int q;
while(m--)
{
scanf("%d",&q);
int ans = ;
int l=,r;
int mn,mx;
mn=mx=a[];
for(r=;r<=n;r++)
{
if(a[r]>mx) mx=a[r];
if(a[r]<mn) mn=a[r];
while(mx-mn>q)
{
l++;
query(l,r,mn,mx);
}
if(ans < r-l+)
ans = r-l+;
}
printf("%d\n",ans);
}
} int main()
{
// freopen("in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF && n)
run();
return ;
}
 
 
 

hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU 4123 Bob’s Race 树的直径 RMQ

    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 树形dp+单调队列

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

  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:树的直径 + 单调队列 + st表

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

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

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

  8. HDU 4123 Bob’s Race(RMQ)

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

  9. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

随机推荐

  1. Struts2 ModelDriven接口使用

    用户在做http请求时一般都有两种方式:get和post方式.get方式用来获取查询相关信息,既向服务器获得信息,而post方式用来更新信息既向服务器提交数据.通常情况下,用get方式向服务器获取信息 ...

  2. sql server单引号和双引号的区别

    --当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔--当 SET QUOTED_IDENTIFIER 为 OFF(默认值)时,表达式中的文 ...

  3. pycharm注册码地址

    (1)地址:http://idea.lanyus.com/ (2)注意,在破解的时候,是先修改hosts文件所在路径:“C:\Windows\System32\drivers\etc\hosts”,修 ...

  4. 九度OJ 1043:Day of Week(星期几) (日期计算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5349 解决:1923 题目描述: We now use the Gregorian style of dating in Russia. ...

  5. Versions 崩溃(Mac升级OS X Yonsemite 10.10)

    今天兴冲冲的升级到了OS X Yonsemite 10.10,结果发现SVN工具不能用了,于是找到一个暂时的解决的方法 1.打开目录~/.subversion/servers 2.在[global] ...

  6. DOM相关操作的案例

    1 . 模态框案例 示例 :  打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 <!DOCTYPE html> <htm ...

  7. LibreOJ 数列分块入门

    题目链接:https://loj.ac/problem/6277 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 nnn. 第 ...

  8. POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-3261 Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Tot ...

  9. Hadoop- 流量汇总程序之如何实现hadoop的序列化接口及代码实现

    流量汇总程序需求 统计每一个用户(手机号)锁耗费的总上行流量.下行流量.总流量. 流程剖析 阶段:map 读取一行数据,切分字段, 抽取手机号,上行流量,下行流量 context.write(手机号, ...

  10. JAVA- 成员变量与局部变量的区别

    成员变量与局部变量的区别 成员变量是定义在方法之外,类之内的局部变量是定义在方法之内的. 作用上的区别: 1.成员变量的作用是用于描述一类事物的公共属性的. 2.局部变量的作用就是提供一个变量给方法内 ...