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个数 ...
随机推荐
- vue基础---介绍
(1)声明式渲染 Vue.js 的核心是采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: ①文本 <div id="app"> {{ message }} & ...
- 08Oracle Database 完整性约束
Oracle Database 完整性约束 非空约束 创建表时 Create table table_name( Column_name datatype NOT NULL,… ); 修改表时 Alt ...
- PHP--选择排序
<?php /** * 选择排序(从小到大)的思想:每一次从待排序的数据中选出最小的,放在待排序的起始位置. */ $arr = array(23, 42, 21, 8, 4, 2, 3, 1) ...
- 服务器的部署与Web项目的发布
今天给老师的服务器部署项目,这次是第二次,基于第一次的经验,这次可以说是驾轻就熟. 服务器的系统是Windows Server 2008 R2 (64位) 需要安装的软件是:jdk7.TomCat7. ...
- session--保持登录20分钟,常用与用户登录状态
思路:request 与 session 两个作对比 1. . . 2. 3.
- CentOS 6 Yum本地源配置
#cd /etc/yum.repos.d #rm CentOS-Base.repo CentOS-Base.repo 是yum 网络源的配置文件(默认) #vi CentOS-Media.repo C ...
- Linux学习笔记(五) 账号管理
1.用户与组账号 用户账号:包括实际人员和逻辑性对象(例如应用程序执行特定工作的账号) 每一个用户账号包含一个唯一的用户 ID 和组 ID 标准用户是系统安装过程中自动创建的用户账号,其中除 root ...
- Python随笔day02
算术运算符 + - * ** / // % 比较运算符 > < == >= <= != Python中提供一种更加简单的比较方式. 当判断 ...
- hdu 1166敌兵布阵(线段树入门题)
>>点击进入原题测试<< 思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记.入门题 cin,cout会超时,记得加std::ios::sync_wit ...
- nyoj 5 Binary String Matching(string)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...