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

时间:2022-04-03 00:15:08

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 树的直径&plus;单调队列

    题意: 给定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&&num;39&semi;s Race:树的直径 &plus; 单调队列 &plus; st表

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

  5. HDU 4123 Bob’s Race 树形dp&plus;单调队列

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

  6. hdu 4123 Bob’s Race &lpar;dfs树上最远距离&plus;RMQ&rpar;

    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&plus;rmq&plus;尺取)

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

随机推荐

  1. struts2上传的问题

    5. 在这里我加一个struts2的一个上传验证的问题 上传时我们可以这样来验证 //判断上传的文件是否合要求 public boolean filterType(String []types){ / ...

  2. Openstack的HA解决方案【mysql集群配置】

    使用mysql的galera做多主集群配置,galera的集群优势网络上面有对比,这里不在叙述. 1. 新建3台虚拟机(centos6.5) node1:172.17.44.163 node2:172 ...

  3. bzoj 3620 似乎在梦中见过的样子(KMP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3620 [题意] 给定一个字符串,统计有多少形如A+B+A的子串,要求A>=K,B ...

  4. &lbrack;MODX&rsqb; 1&period; Template &ast;

    After uploading javascript, css and images to the assets folder. We try to use Template to customize ...

  5. Spring配置bean的详细知识

    在Spring中配置bean的一些细节.具体信息请参考下面的代码及注释 applicationContext.xml文件 <?xml version="1.0" encodi ...

  6. CQRS学习——IOC,配置,仓储隔离以及QueryEntry&lbrack;其三&rsqb;

    从IoC开始说起 博主最早开始用的IoC容器叫AutoFac,那时候用它主要是为了生命周期管理——将EF上下文的生命周期限定为每请求.当然也总是每每听到IoC的好处,但是仍然不能理解其优势.最近在学习 ...

  7. JQuery判断子Iframe 加载完成的技术解决

    当需要我们给当前页面动态创建Iframe子框架的时候,并且同时需要操作子Iframe里的方法的时候,我们发现无法成功实现.这是为什么呢?经小程总结,发现子Iframe还没有来的及加载完成,就去执行里面 ...

  8. PDF 补丁丁 0&period;6&period;0&period;3383 版发布(修复书签编辑器坐标定位错误的问题)

    新的测试版本修复了书签编辑器坐标定位错误的问题. 另外,增加了鼠标双击关闭功能标签的功能.

  9. &lbrack;SDOI2010&rsqb;捉迷藏

    嘟嘟嘟 k-d tree板儿题. 建完树后对每一个点求一遍最小和最大曼哈顿距离,是曼哈顿,不是欧几里得. #include<cstdio> #include<iostream> ...

  10. 【斜优DP】bzoj4518-Sdoi2016征途

    一.斜率优化DP与决策单调性 这里浅显(并且不严谨)地说明一下标题中的两个名词: 斜率优化DP:状态转移方程形如f[i]=min/max{f[k]+(x[i]-x[k])^y}的一类DP问题: 决策单 ...