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 ...
随机推荐
- python 基础 9.1 连接数据库
二.数据库连接 MySQLdb 提供了connect 方法用来和数据库建立连接,接收数个参数,返回连接对象: #/usr/bin/python #coding=utf-8 #@Time :2017 ...
- 【BZOJ4826】[Hnoi2017]影魔 单调栈+扫描线
[BZOJ4826][Hnoi2017]影魔 Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝 ...
- Angular入门(四) Router 替换当前页面
1.在 xx.html 中直接 写标签 <a [routerLink]="['/home']">home</a> 2.在 xx.html 中 ...
- protect,internal的区别
protected: 爷爷有一张银行卡,爸爸可以用,儿子也可以用,隔壁老王不可以用(因为老王跟爷爷没有继承关系) internal: 王总有一张银行卡,秘书可以用,经理可以用,王总儿子不可以用(因为银 ...
- table表格隔行变色
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Flask:程序结构
在Flask中需要配置各种各样的参数.比如设置秘钥,比如上一章介绍到的配置数据库类型. app.config['SECRET_KEY']=os.urandom(20) app.config['SQLA ...
- Eclipse for PHP Developers使用笔记
1 修改字体样式:Window-->Preference-->General-->Appearance-->Basic-->text font-->edit
- 磁卡ID卡IC卡的区别【转】
本文转载自:https://blog.csdn.net/trap94/article/details/50614451 今天被一个朋友问到ID卡和IC卡有什么区别,还真给问住了.虽然平时经常用到这些卡 ...
- Linux内核--并发【转】
本文转自自:http://www.jianshu.com/p/035550ae05d2 为什么会产生并发 1.多个用户同时登陆的时候,他们有可能在任何时刻以任意的组合调用内核代码. 2.smp系统可能 ...
- 城市旅游ppt模板
城市旅游ppt模板,城市,旅游,旅行,休闲. 下载:http://www.huiyi8.com/lvyoumuban/ppt/