Bob’s Race

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
Source
题意:给你一个树,求每个点的最远距离,并且最最大的区间长度小于等于q;
思路:1:树的直径
         2:rmq的st表;
   3:尺取;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
struct is
{
int v,w,nex;
}edge[N];
int head[N],edg;
int node1,node2,deep;
int dis[N],num[N];
int pos[N];
void init()
{
memset(num,,sizeof(num));
memset(head,-,sizeof(head));
memset(dis,,sizeof(dis));
edg=;
deep=;
}
void add(int u,int v,int w)
{
edg++;
edge[edg].v=v;
edge[edg].w=w;
edge[edg].nex=head[u];
head[u]=edg;
}
void dfs(int u,int fa,int val,int &node)
{
dis[u]=max(dis[u],val);
if(val>deep)
{
deep=val;
node=u;
}
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
int w=edge[i].w;
if(v==fa)continue;
dfs(v,u,val+w,node);
}
}
int dpi[N][];
int dpa[N][];
int minn(int x,int y)
{
return num[x]<=num[y]?x:y;
}
void rmqi(int len)
{
for(int i=; i<len; i++)
dpi[i][]=i;
for(int j=; (<<j)<len; j++)
for(int i=; i+(<<j)-<len; i++)
dpi[i][j]=minn(dpi[i][j-],dpi[i+(<<(j-))][j-]);
}
int queryi(int l,int r)
{
int x=pos[r-l+];
return minn(dpi[l][x],dpi[r-(<<x)+][x]);
}
int maxx(int x,int y)
{
return num[x]>=num[y]?x:y;
}
void rmqa(int len)
{
for(int i=; i<len; i++)
dpa[i][]=i;
for(int j=; (<<j)<len; j++)
for(int i=; i+(<<j)-<len; i++)
dpa[i][j]=maxx(dpa[i][j-],dpa[i+(<<(j-))][j-]);
}
int querya(int l,int r)
{
int x=pos[r-l+];
return maxx(dpa[l][x],dpa[r-(<<x)+][x]);
}
int n,m;
int main()
{
pos[]=-;
for(int i=;i<;i++) pos[i]=pos[i>>]+;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==)break;
init();
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dfs(,-,,node1);
deep=;
dfs(node1,-,,node2);
dfs(node2,-,,node1);
for(int i=;i<=n;i++)
num[i]=max(dis[i],num[i]);
rmqi(n+);
rmqa(n+);
while(m--)
{
int z;
scanf("%d",&z);
int l=,r=,ans=;
while()
{
while(num[querya(l,r)]-num[queryi(l,r)]<=z&&r<=n)r++;
ans=max(ans,r-l);
if(r>n)
break;
l++;
}
printf("%d\n",ans);
}
}
return ;
}

hdu 4123 Bob’s Race 树的直径+rmq+尺取的更多相关文章

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

  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. hdu4123-Bob’s Race(树形dp+rmq+尺取)

    题意:Bob想要开一个运动会,有n个房子和n-1条路(一棵树),Bob希望每个人都从不同的房子开始跑,要求跑的尽可能远,而且每条路只能走最多一次.Bob希望所有人跑的距离的极差不大于q,如果起点的编号 ...

随机推荐

  1. SpringMVC @RequestBody接收Json对象字符串 demo

    springmvc 的这个 @RequestBody 用得比较少,今天看了一下,还是很方便. @RequestBody 接收类似 [{name: "test"}, {name: & ...

  2. javaWeb 使用jsp开发 if else 标签

    1.jsp页面调用代码 <t:choose> <t:when test="${user==null }">还没有登录</t:when> < ...

  3. Hibernate,JPA注解@Version

    Hibernate实现悲观锁和乐观锁. 1,悲观锁 用例代码如下: 数据库DDL语句: hibernate.cfg.xml java类 以上代码(除下面的main之外)同乐观锁. main packa ...

  4. PHP多表取数据的代码优化

    <?php header("Content-type: text/html; charset=utf-8"); //假设这里的$goods_arr  和 $shop_arr  ...

  5. ecshop的几个小瑕疵

    在安装Ecshop的时候,遇到两个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be cal ...

  6. Python 字典(Dictionary)

    字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = ...

  7. c# 回调委托

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. java文件写入和读出的序列化

    文件的写入入与读出都有它们自己的格式,不便于读入和取出,implement Serializable接口,实现任何个事文件的写入和读取取:

  9. php时间函数整理

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  10. drawer principle in Combinatorics

    Problem 1: Given an array of real number with length (n2 + 1) A: a1,  a2, ... , an2+1. Prove that th ...