Bob’s Race

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3741    Accepted Submission(s): 1206

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

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int N=+;
int head[N];
int dp1[N][],dp2[N][];
int tot;
int n;
struct node{
int to,next,w;
}edge[N<<];
int dp[N][];
int vis[N];
int a[N];
void init(){
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
tot=;
}
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].w=w;
head[u]=tot++;
}
void DFS(int u,int fa){
int maxx1=;
int maxx2=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
int w=edge[i].w;
if(v==fa)continue;
DFS(v,u);
int maxx=dp[v][]+w;
if(maxx>=maxx1){
maxx2=maxx1;
maxx1=maxx;
vis[u]=v;
}
else if(maxx>maxx2){
maxx2=maxx;
}
// cout<<u<<" "<<v<<" "<<maxx1<<" "<<maxx2<<endl;
}
dp[u][]=maxx1;
dp[u][]=maxx2;
}
void DFS1(int u,int fa){
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
int w=edge[i].w;
if(v==fa)continue;
if(vis[u]==v){
dp[v][]=max(dp[u][]+w,dp[u][]+w);
}
else{
dp[v][]=max(dp[u][]+w,dp[u][]+w);
}
DFS1(v,u);
}
}
void init_RMQ(){
for(int i=;i<=n;i++){
dp1[i][]=a[i];
dp2[i][]=a[i];
}
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++){
dp1[i][j]=max(dp1[i][j-],dp1[i+(<<(j-))][j-]);
dp2[i][j]=min(dp2[i][j-],dp2[i+(<<(j-))][j-]);
}
}
int getRMQ(int L,int R){
int k=;
while((<<(k+))<=R-L+)k++;
int maxx=max(dp1[L][k],dp1[R-(<<k)+][k]);
int minn=min(dp2[L][k],dp2[R-(<<k)+][k]);
return maxx-minn;
} int main(){
int m;
while(scanf("%d%d",&n,&m)!=EOF){ if(m==&&n==)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(,-);
DFS1(,-);
for(int i=;i<=n;i++){
a[i]=max(dp[i][],dp[i][]);
//cout<<a[i]<<" ";
}
init_RMQ();
for(int i=;i<m;i++){
int x;
scanf("%d",&x);
int l=;
int r=;
int ans=;
while(l<=n&&r<=n){
while(r<=n&&getRMQ(l,r)<=x){
r++;
}
ans=max(ans,r-l);
l++;
}
cout<<ans<<endl;
}
}
}

hdu 4123(树形dp+倍增)的更多相关文章

  1. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  2. hdu 4123 树形DP+单调队列

    http://acm.hust.edu.cn/vjudge/problem/25790 这题基本同poj 3162 要注意mx,mx2,vx,vx2每次都要初始化 #include <iostr ...

  3. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  4. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  5. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]

    题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...

  7. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  8. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  9. codevs 1380/HDU 1520 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...

  10. HDU 5834 [树形dp]

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

随机推荐

  1. pl/sql编程语言

    –pl/sql编程语言–pl/sql编程语言是对sql语言的扩展,是的sql语言具有过程化编程的特性–pl/sql编程语言比一般的过程化编程语言,更加灵活高效–pl/sql编程语言主要用来编写存储过程 ...

  2. 使用SELECT语句检索数据

    使用SELECT语句检索数据select指令适用于SQL数据库SELECT 语句用于从数据库中选取数据.(指令不分大小写,选择的值除名字和一些有特殊意义的字符可不分大小写,from结束时一定要加;) ...

  3. Apache 和 Nginx 下的 URL 重写

    URL 重写和重定向 URL 重写是将页面映射到本站另一页面, 而重定向则是将页面映射到另一主机(域名). 其中临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的, 是 SEO 的重要 ...

  4. vue中websoket的使用

    首先安装npm install --save  websocket-heartbeat-js@^1.0.7 在main.js中  引入并挂载全局方法 import WebsocketHeartbeat ...

  5. Loadrunner12 安装与卸载

    卸载:http://www.51testing.com/html/21/303921-216608.html 安装包下载: loadrunner12安装包下载:链接:https://pan.baidu ...

  6. @Inherited注解

    允许子类继承父类的注解 https://blog.csdn.net/renli2549/article/details/78432272

  7. 暴力搜索+散列--P1008 三连击

    题目描述 将1,2, ⋯,9共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行,每行3 ...

  8. 58.海量bucket优化机制:从深度优先到广度优先

    当buckets数量特别多的时候,深度优先和广度优先的原理,图解 假如我们有如下数据数据:每个演员的每个电影的评论. 现在我们的需求是找到前10名的演员所演的电影的评论.这是一个两层聚合题.     ...

  9. sql学习笔记:表的运算

    在MICK的<SQL基础教程>里读到的一章,写的很好,之前很乱的思路变清晰了很多.简单来说,表的运算主要是两种:列的运算和行的运算. 表的加减法 这里是对表的列操作(向下扩展).因此,按照 ...

  10. 洛谷 3178 [HAOI2015]树上操作

    [题解] 就是个树链剖分的模板题. #include<cstdio> #include<algorithm> #include<cstring> #define L ...