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=4123
Description
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
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
HINT
题意
一个城镇有N个住户,N-1条路连接两个住户,保证N个住户联通,M次询问,给定N条边的信息,包括连
接的住户序号以及路的长度。然后是M次询问,每次询问Q,要求找到最长的连续序号(我一开始看成了树上连续一段),使得Max(dis[i]) - Min(dis[i]) ≤
Q(l≤i≤r),输出最大的r-l+1。dis[i]为从第i个住户出发,不重复走过路能移动的最远距离。
题解:
1.首先需要知道一个 结论:树上每个节点理他最远的点肯定是数的直径的两个端点的其中一个。
2.如果你不会求树的直径以及两个端点,没关系,其实不难,任意找一个点dfs找到离它最远的点root1,root1一定是其中一个端点,然后再以root1为根dfs一遍求出理root1最远的点root2。这样就找到了两个端点。
3.求出每个点到最远点的距离,我这里是分别以root1,root2 dfs,求出每个点到root1和root2的距离,取个max即可。
4.题目转化成了,求最长的区间(l,r),满足max{l,r}-min{l,r}<=q。
5.其实可以用尺取法来做,因为假如(l,r)区间合法,那么(l+1,r)一定合法,那么枚举左端点,右端点只能单增,时间复杂度是O(n)的。
6.最后要搞个ST表求区间最大最小值。
我一开始用优先队列做的,然后T了,真是花样作死,非要加个log。
衷心提示:k=(int)(log(y-x+1)/2)会超时,这个需要预处理,亲测一个1.5s,另一个5.2秒。
优先队列tle代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 50050
int n,m,val[N],dp1[N],dp2[N],flag[N];
int tot,last[N];
struct Edge{int from,to,val,s;}edges[N<<];
struct Node
{
int id,val;
bool operator <(const Node&b)const
{return val<b.val;}
};
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge (int x,int y,int z)
{
edges[++tot]=Edge{x,y,z,last[x]};
last[x]=tot;
}
void dfs1(int x,int pre,int &mx,int &root)
{
mx=; root=x;
int tpmx,tproot;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dfs1(e.to,x,tpmx,tproot);
if (tpmx+e.val>mx)
{
mx=tpmx+e.val;
root=tproot;
}
}
}
void dfs2(int x,int pre,int *dp)
{
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dp[e.to]=dp[x]+e.val;
dfs2(e.to,x,dp);
}
} void init()
{
read(n); read(m);
if (n==&&m==)exit();
for(int i=;i<=n-;i++)
{
int x,y,z;
read(x); read(y); read(z);
AddEdge(x,y,z);
AddEdge(y,x,z);
}
int maxn=,root1,root2;
dfs1(,,maxn,root1);
dfs1(root1,,maxn,root2);
dfs2(root1,,dp1);
dfs2(root2,,dp2);
for(int i=;i<=n;i++) val[i]=max(dp1[i],dp2[i]);
}
void solve()
{
int r=,ans=,q;
read(q);
priority_queue<Node>Qmx,Qmi;
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
while(r+<=n&&(Qmx.empty()||
(fabs(val[r+]-Qmx.top().val)
<=q&&fabs(val[r+]+Qmi.top().val)<=q)))
{
Qmx.push(Node{r+,val[r+]});
Qmi.push(Node{r+,-val[r+]});
r++;
}
ans=max(ans,r-i+);
flag[i]=;
while(!Qmx.empty()&&flag[Qmx.top().id])Qmx.pop();
while(!Qmi.empty()&&flag[Qmi.top().id])Qmi.pop();
}
printf("%d\n",ans);
}
void clear()
{
tot=;
memset(last,,sizeof(last));
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(flag,,sizeof(flag));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while()
{
clear();
init();
for(int i=;i<=m;i++) solve();
}
}
ST表AC代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 50050
int n,m,dp1[N],dp2[N],mx[N][],mi[N][],mm[N];
int tot,last[N];
struct Edge{int from,to,val,s;}edges[N<<];
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge (int x,int y,int z)
{
edges[++tot]=Edge{x,y,z,last[x]};
last[x]=tot;
}
void dfs1(int x,int pre,int &mx,int &root)
{
mx=; root=x;
int tpmx,tproot;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dfs1(e.to,x,tpmx,tproot);
if (tpmx+e.val>mx)
{
mx=tpmx+e.val;
root=tproot;
}
}
}
void dfs2(int x,int pre,int *dp)
{
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dp[e.to]=dp[x]+e.val;
dfs2(e.to,x,dp);
}
} void init()
{
read(n); read(m);
if (n==&&m==)exit();
for(int i=;i<=n-;i++)
{
int x,y,z;
read(x); read(y); read(z);
AddEdge(x,y,z);
AddEdge(y,x,z);
}
int maxn=,root1,root2;
dfs1(,,maxn,root1);
dfs1(root1,,maxn,root2);
dfs2(root1,,dp1);
dfs2(root2,,dp2);
for(int i=;i<=n;i++) mx[i][]=mi[i][]=max(dp1[i],dp2[i]);
}
void init_ST()
{
mm[]=-;
for(int i=;i<=n;i++)mm[i]=(i&(i-))==?mm[i-]+:mm[i-];
for(int i=;i<=;i++)
for(int j=;j+(<<i)-<=n;j++)
{
mx[j][i]=max(mx[j][i-],mx[j+(<<(i-))][i-]);
mi[j][i]=min(mi[j][i-],mi[j+(<<(i-))][i-]);
}
}
int rmq(int x,int y)
{
int k=mm[y-x+];
int ans=max(mx[x][k],mx[y-(<<k)+][k]);
ans-=min(mi[x][k],mi[y-(<<k)+][k]);
return ans;
}
void solve()
{
int r=,ans=,q;
read(q);
for(int i=;i<=n;i++)
{
while(r+<=n&&rmq(i,r+)<=q)r++;
ans=max(ans,r-i+);
}
printf("%d\n",ans);
}
void clear()
{
tot=;
memset(last,,sizeof(last));
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
clock_t now=clock();
while()
{
clear();
init();
init_ST();
for(int i=;i<=m;i++) solve();
} }
HDU 4123 Bob’s Race 树的直径+ST表的更多相关文章
- 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 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- HDU 4123 Bob’s Race 树的直径+单调队列
题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...
- [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)
[51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...
- 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 ...
- 刷题总结——Bob's Race(hdu4123 树形dp+st表)
题目: Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the ro ...
随机推荐
- 【luoguP2827】 蚯蚓
题目描述 本题中,我们将用符号\rfloor⌊c⌋ 表示对 cc 向下取整,例如:\lfloor 3.0 \rfloor = \lfloor 3.1 \rfloor = \lfloor 3.9 \rf ...
- node中的http内置模块
Node.js开发的目的就是为了用JavaScript编写Web服务器程序.因为JavaScript实际上已经统治了浏览器端的脚本,其优势就是有世界上数量最多的前端开发人员.如果已经掌握了JavaSc ...
- Pytest学习笔记(三) 在代码中运行pytest
前面介绍的是在cmd中执行pytest,平常我们一般都是通过编译器(如pycharm)来编写用例的,写完用例后,需要调试看看是否能运行,如果每次都切换到cmd中执行,太麻烦. 因此,这一节来说下怎么在 ...
- python3版 爬虫了解
摘要:本文将使用Python3.4爬网页.爬图片.自动登录.并对HTTP协议做了一个简单的介绍.在进行爬虫之前,先简单来进行一个HTTP协议的讲解,这样下面再来进行爬虫就是理解更加清楚. 一.HTTP ...
- dsu on tree(树上启发式合并)
简介 对于一颗静态树,O(nlogn)时间内处理子树的统计问题.是一种优雅的暴力. 算法思想 很显然,朴素做法下,对于每颗子树对其进行统计的时间复杂度是平方级别的.考虑对树进行一个重链剖分.虽然都基于 ...
- openfalcon架构及相关服务配置详解(转)
一:openfalcon组件 1.falcon-agent 数据采集组件 agent内置了一个http接口,会自动采集预先定义的各种采集项,每隔60秒,push到transfer. 2.transfe ...
- Python 调用JS文件中的函数
Python 调用JS文件中的函数 1.安装PyExecJS第三方库 2.导入库:import execjs 3.调用JS文件中的方法 Passwd = execjs.compile(open(r&q ...
- hadoop用户和权限
当前Apache Hadoop认证(authentication)支持simple和kerberos,simple是默认的,其实是信任操作系统的认证结果(也就是直接使用操作系统的用户).kerbero ...
- koa 项目实战(十一)验证登录和注册的 input
1.验证注册参数 根目录/validation/register.js const Validator = require('validator'); const isEmpty = require( ...
- python 操作excel openpyxl
1 安装 pip install openpyxl 如果装不上,请指定安装源来安装 pip install -i https://pypi.douban.com/simple openpyxl 如果e ...