hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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)
1 2 3
2 3 4
4 5 3
3 4 2
1
2
3
4
5
0 0
3
3
3
5
#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+尺取的更多相关文章
- 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 ...
- HDU 4123 Bob’s Race 树的直径+单调队列
题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...
- 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 ...
- HDU 4123 Bob's Race:树的直径 + 单调队列 + st表
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...
- HDU 4123 Bob’s Race 树形dp+单调队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...
- hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)
C - Bob’s Race Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- 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 ...
- HDU 4123 Bob’s Race(RMQ)
题意是说给出一棵树,N(10^5)个顶点,以及每条边的权值,现在需要选择连续的K个点(顶点编号连续),可以被选出来的条件是: 若d[i]代表顶点i到树上其他点的距离的最大值,使得区间[a, b]的d值 ...
- hdu4123-Bob’s Race(树形dp+rmq+尺取)
题意:Bob想要开一个运动会,有n个房子和n-1条路(一棵树),Bob希望每个人都从不同的房子开始跑,要求跑的尽可能远,而且每条路只能走最多一次.Bob希望所有人跑的距离的极差不大于q,如果起点的编号 ...
随机推荐
- python logging 替代print 输出内容到控制台和重定向到文件
转自:http://blog.csdn.net/z_johnny/article/details/50740528
- 【python cookbook】【数据结构与算法】17.从字典中提取子集
问题:想创建一个字典,其本身是另一个字典的子集 解决方案:利用字典推导式(dictionary comprehension)可轻松解决 # example of extracting a subset ...
- linux内核中异步通信机制--信号处理机制【转】
转自:http://blog.csdn.net/lu_embedded/article/details/51131663 什么是异步通信?很简单,一旦设备准备好,就主动通知应用程序,这种情况下应用程序 ...
- Mysql slow query log
一.概念部分: 顾名思义,慢查询日志中记录的是执行时间较长的query,也就是我们常说的slow query,通过设--log-slow-queries[=file_name]来打开该功能并设置记录 ...
- Codeforces 733C:Epidemic in Monstropolis(暴力贪心)
http://codeforces.com/problemset/problem/733/C 题意:给出一个序列的怪兽体积 ai,怪兽只能吃相邻的怪兽,并且只有体积严格大于相邻的怪兽才能吃,吃完之后, ...
- C#的OpenFileDialog和SaveFileDialog的常见用法(转)
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = ...
- Winform知识点
Winform窗体常用的控件 1.按钮 Button 前缀:btn 2.文本框 TextBox 前缀:txt 3.单选按钮 RadioButton 前缀:rdo 4.复选按钮 CheckBox 前缀: ...
- ant-环境变量
ant是绿色免安装的,通常习惯放在C盘根目录下,如:C:\apache-ant-1.9.4 那么环境就应该配置为: ANT_HOME=C:\apache-ant-1.9.4(注意:这里不需要加分号)P ...
- VS2015+Windows 10下配置opencv3.0
博客园样式换了,原先的文章排版太乱了,新发一篇 ##0. 安装 opencv 3.0 首先去官网下载安装包(其实是个压缩包),释放到随便的一个路径里面为了方便描述,下面把这个路径称为 cvPath. ...
- ExecutorService - 10个技巧和窍门
ExecutorService已经成为Java并发编程中常用的基础库,几乎所有到线程 任务等执行都要委托ExecutorService.下面是使用过程中10个技巧和窍门. 1.为线程池和线程取名 当我 ...