hdu 4123(树形dp+倍增)
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
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)
#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+倍增)的更多相关文章
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- hdu 4123 树形DP+单调队列
http://acm.hust.edu.cn/vjudge/problem/25790 这题基本同poj 3162 要注意mx,mx2,vx,vx2每次都要初始化 #include <iostr ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- HDU 1561 树形DP入门
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]
题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- codevs 1380/HDU 1520 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...
- HDU 5834 [树形dp]
/* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...
随机推荐
- vs2013打包
C#打包需要这个:InstallShield 2013 Limited Edition for Visual Studio . 下载地址: InstallShield 2013 Limited Ed ...
- Python 字符串常见的用法
line = “ni hao wo jiao key” line.capotalize()#首字母大写 line.center(20)#居中显示固定的字符 line.count('n')#计数,计算该 ...
- 微信支付开发 c#
代码demo下载地址: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
- 使用yolo3模型训练自己的数据集
使用yolo3模型训练自己的数据集 本项目地址:https://github.com/Cw-zero/Retrain-yolo3 一.运行环境 1. Ubuntu16.04. 2. TensorFlo ...
- Linux 应用总结:自动删除n天前的日志
linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...
- CentOS7安装Nginx及其相关
一.安装所需环境 gcc 安装 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装. yum install gcc-c++ PCRE pcr ...
- Android jdbc连接mysql报错解决方案 (Communications link failure)
最近调试安卓连接mysql真是心态爆炸,快两天才搞出来.以下整理一些常见问题. 检查manifest文件里网络权限是否打开 检查数据库IP是否有问题(包括一些沙雕错误,比如是不是在ip首或尾多了个空格 ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
- BZOJ 3894 Luogu P4313 文理分科 (最小割)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3894 (luogu) https://www.luogu.org/pro ...
- javaHttp请求,接收到的是中文乱码如何处理
可在service()方法中加日志,看哪种不是乱码 例如,中文乱码的话,中文编码一般有 UTF-8,GBK,ISO-8859-1 加日志为 List<String> list = new ...