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. python 基础 9.1 连接数据库

    二.数据库连接 MySQLdb 提供了connect 方法用来和数据库建立连接,接收数个参数,返回连接对象: #/usr/bin/python #coding=utf-8 #@Time   :2017 ...

  2. 【BZOJ4826】[Hnoi2017]影魔 单调栈+扫描线

    [BZOJ4826][Hnoi2017]影魔 Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝 ...

  3. Angular入门(四) Router 替换当前页面

    1.在 xx.html 中直接 写标签       <a [routerLink]="['/home']">home</a>   2.在 xx.html 中 ...

  4. protect,internal的区别

    protected: 爷爷有一张银行卡,爸爸可以用,儿子也可以用,隔壁老王不可以用(因为老王跟爷爷没有继承关系) internal: 王总有一张银行卡,秘书可以用,经理可以用,王总儿子不可以用(因为银 ...

  5. table表格隔行变色

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Flask:程序结构

    在Flask中需要配置各种各样的参数.比如设置秘钥,比如上一章介绍到的配置数据库类型. app.config['SECRET_KEY']=os.urandom(20) app.config['SQLA ...

  7. Eclipse for PHP Developers使用笔记

    1 修改字体样式:Window-->Preference-->General-->Appearance-->Basic-->text font-->edit

  8. 磁卡ID卡IC卡的区别【转】

    本文转载自:https://blog.csdn.net/trap94/article/details/50614451 今天被一个朋友问到ID卡和IC卡有什么区别,还真给问住了.虽然平时经常用到这些卡 ...

  9. Linux内核--并发【转】

    本文转自自:http://www.jianshu.com/p/035550ae05d2 为什么会产生并发 1.多个用户同时登陆的时候,他们有可能在任何时刻以任意的组合调用内核代码. 2.smp系统可能 ...

  10. 城市旅游ppt模板

    城市旅游ppt模板,城市,旅游,旅行,休闲. 下载:http://www.huiyi8.com/lvyoumuban/ppt/