hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
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
Sample Input
1 2 3
2 3 4
4 5 3
3 4 2
1
2
3
4
5
0 0
Sample Output
3
3
3
5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = ; struct _edge{
int to,w,next;
};
_edge edge[N<<];
int ecnt,head[N];
inline void addedge(int u,int v,int w)
{
edge[ecnt].to = v;
edge[ecnt].w = w;
edge[ecnt].next = head[u];
head[u]=ecnt++;
} int n,m,a[N];
int dp[N][],id[N][]; void dfs1(int u,int fa)
{
dp[u][]=dp[u][]=;
for(int e=head[u];e!=-;e=edge[e].next)
{
int &v = edge[e].to;
if(v==fa) continue;
dfs1(v,u);
int t1 = dp[v][] + edge[e].w, t2 = v;
if(t1>=dp[u][])
{
swap(t1,dp[u][]);
swap(t2,id[u][]);
}
if(t1>=dp[u][])
{
swap(t1,dp[u][]);
swap(t2,id[u][]);
}
}
} void dfs2(int u,int fa,int up)
{
a[u]=max(dp[u][],up);
for(int e=head[u];e!=-;e=edge[e].next)
{
int &v = edge[e].to;
if(v==fa) continue;
int t;
if(v==id[u][])
t = max(dp[u][],up);
else
t = max(dp[u][],up);
dfs2(v,u,t+edge[e].w);
}
} int dmx[N][],dmn[N][];
void RMQ_init()
{
for(int i=;i<=n;i++) dmn[i][]=dmx[i][]=a[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dmn[i][j]=min(dmn[i][j-],dmn[i+(<<(j-))][j-]),
dmx[i][j]=max(dmx[i][j-],dmx[i+(<<(j-))][j-]);
}
void query(int l,int r,int &mn,int &mx)
{
int k = ;
while((<<(k+))<=r-l+) k++;
mn = min(dmn[l][k],dmn[r-(<<k)+][k]);
mx = max(dmx[l][k],dmx[r-(<<k)+][k]);
} void run()
{
int u,v,w;
ecnt=;
memset(head,-,sizeof(head));
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
memset(dp,-,sizeof(dp));
memset(id,-,sizeof(id));
dfs1(,-);
dfs2(,-,);
// for(int i=1;i<=n;i++)
// cout<<a[i]<<' ';
// cout<<endl; RMQ_init(); int q;
while(m--)
{
scanf("%d",&q);
int ans = ;
int l=,r;
int mn,mx;
mn=mx=a[];
for(r=;r<=n;r++)
{
if(a[r]>mx) mx=a[r];
if(a[r]<mn) mn=a[r];
while(mx-mn>q)
{
l++;
query(l,r,mn,mx);
}
if(ans < r-l+)
ans = r-l+;
}
printf("%d\n",ans);
}
} int main()
{
// freopen("in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF && n)
run();
return ;
}
hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)的更多相关文章
- 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 树的直径+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 树的直径 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 树形dp+单调队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...
- 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:树的直径 + 单调队列 + st表
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...
- HDU 4123 Bob’s Race 树的直径+单调队列
题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...
- HDU 4123 Bob’s Race(RMQ)
题意是说给出一棵树,N(10^5)个顶点,以及每条边的权值,现在需要选择连续的K个点(顶点编号连续),可以被选出来的条件是: 若d[i]代表顶点i到树上其他点的距离的最大值,使得区间[a, b]的d值 ...
- POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)
Bob’s Race Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 378 Accepted: 119 Descript ...
随机推荐
- 九度OJ 1036:Old Bill (老比尔) (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2691 解决:1432 题目描述: Among grandfather's papers a bill was found. 72 ...
- (1)Web 应用是一个状态机,视图与状态是一一对应的。 (2)所有的状态,保存在一个对象里面。
Redux 入门教程(一):基本用法 - 阮一峰的网络日志 http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_u ...
- 交易准实时预警 kafka topic 主题 异常交易主题 低延迟 event topic alert topic 内存 算法测试
https://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html 周 明耀2015 年 6 月 10 日发布 示例:网络游 ...
- UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- PR 批量导入
REPORT ZMM_UPLOAD_PR. DATA: BEGIN OF GT_DATA1 OCCURS 0, BSART TYPE STRING, "凭证类型 ...
- yun 安装mysql
1.安装客户端和服务器端 确认mysql是否已安装: yum list installed mysql* rpm -qa | grep mysql* 查看是否有安装包: yum list mysq ...
- JETSON TK1 ~ 控制GPIO
首先建立个存放gpio代码的文件夹,CD到该文件夹. git clone git://github.com/derekmolloy/boneDeviceTree/ 解压后会出现几个文件 GPIO文件夹 ...
- CEF与MLS快速交换对比
MLS快速交换 到达某特定目的地址的IP包通常会引起数据包流,即假设交换过到特定目标的包之后,另一个很可能不久也会到达.通过构建最近交换目标的高速缓存,可以减少包在全路由表中查找同一目标的次数,这种“ ...
- ultraedit激活
使用期满的解决办法:https://blog.csdn.net/dfh00l/article/details/52093630 下载:https://blog.csdn.net/qq_16093323 ...
- matlab unique()函数
a = [30, 3,6; 19, 20, 20; 30,3, 6;] b=unique(a,'rows') 返回结果是: b = [19, 20 ,20; 30, 3, 6] b=uniqu ...