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. javaWeb 使用jsp标签进行防盗链

    /** * 1.新建类继承SimpleTagSupport * 新建2个属性, 添加对应的set方法 * 覆盖doTag()方法 */ import java.io.IOException; impo ...

  2. C# Community Projects

    Community Supported C# Drivers See the officially supported MongoDB C# driver mongodb-csharp driver ...

  3. 转载WPF SDK研究 之 AppModel

    Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...

  4. Ganglia监控MySQL

    1.下载mysql监控脚本: [root@node1 app]# wget http://www.javabloger.com/att/gmetric-mysql.sh 2.修改脚本中的msyql用户 ...

  5. Nagios监控Oralce

    一.本文说明: 本文是监控本地的Oracle,其实监控远端的Oracle也是跟下面的步骤差不多的. 二.安装Nagios.Nagios插件.NRPE软件: 安装步骤可以参考<Linux下Nagi ...

  6. css常用命名规则

    (一)常用的CSS命名规则 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrappe ...

  7. asp.net 分页-利用后台直接生成html分页

    一直想做一个属于自己的分页, 1:我试过用datapage,虽然是很好用,但是必须要配合datalist才能使用,感觉不太好 2:自己写分页控件,目前正在摸索中,关键是怎么分页的问题,有的是用data ...

  8. recycleview + checkbox 实现单选

    使用map集合记录checkbox的选中状态 private HashMap<Integer,Boolean> positionMap; positionMap = new HashMap ...

  9. 【转】MYSQL入门学习之十一:触发器的基本操作

    转载地址:http://www.2cto.com/database/201212/176781.html 触发器是MySQL响应以下任意语句而自动执行的一条MySQL语句(或位于BEGIN和END语句 ...

  10. Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏

    Self Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22101   Accepted: 12429 De ...