还没有学过RMQ,所以只能用会的单调队列做。

Bob’s Race

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

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
 
Recommend
lcy
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 50050
int n,m;
struct node
{
int to,next,w;
}edge[*N]; struct qq
{
int time,key;
}que[N],que1[N]; int cnt,pre[N];
int g[N];
int mx,mi;
int dp[N][];//记录一次dfs中子树的最大值和次大值
int save[N]; //记录每个点到相邻点最大路程的id号 void add_edge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=pre[u];
pre[u]=cnt++;
} int dfs(int s,int path)
{
for(int p=pre[s];p!=-;p=edge[p].next)
{
int v=edge[p].to;
if(v!=path)
{
int tmp=dfs(v,s)+edge[p].w;
if(tmp>=dp[s][])
{
dp[s][]=dp[s][];
dp[s][]=tmp;
save[s]=v;
}
else
{
if(tmp>dp[s][])
dp[s][]=tmp;
}
}
}
return dp[s][];
} void dfs1(int s,int sum,int path)
{
if(sum>=dp[s][])
{
dp[s][]=dp[s][];
dp[s][]=sum;
save[s]=path;
}
else if(sum>dp[s][]) dp[s][]=sum;
g[s]=dp[s][];
for(int p=pre[s];p!=-;p=edge[p].next)
{
int v=edge[p].to;
if(v!=path)
{
if(save[s]==v) dfs1(v,dp[s][]+edge[p].w,s);
else dfs1(v,dp[s][]+edge[p].w,s);
}
}
}
int main()
{
//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
while(scanf("%d%d",&n,&m)&&(m+n))
{
cnt=;
memset(pre,-,sizeof(pre));
for(int i=;i<n;i++)
{
int x,y,key;
scanf("%d%d%d",&x,&y,&key);
add_edge(x,y,key);
add_edge(y,x,key);
}
memset(dp,,sizeof(dp));
dfs(,);//记录好
dfs1(,,);//这样就可以求出每个出发的最长路
/*
for(int i=1;i<=n;i++)
printf("%d ",g[i]);
printf("\n");
*/
int qf,qd;
int qf1,qd1;
for(int ii=;ii<m;ii++)
{
int ans=,lim;
scanf("%d",&lim);
mx=g[]; mi=g[];
qf=qd=qf1=qd1=;
int k=;
int tans=;
que[qf].key=g[]; que[qf].time=; qf++;
que1[qf1].key=g[]; que1[qf1].time=; qf1++;
for(int i=;i<=n;i++)
{
while(qf>qd&&que[qf-].key<=g[i]) qf--;
que[qf].time=i;
que[qf].key=g[i];
qf++;
//////
while(qf1>qd1&&que1[qf1-].key>=g[i]) qf1--;
que1[qf1].time=i;
que1[qf1].key=g[i];
qf1++;
mx=que[qd].key;
mi=que1[qd1].key;
tans++;
if(mx-mi<=lim)
{
if(tans>ans) ans=tans;
}
else
{
while(mx-mi>lim)
{
k++;
tans--;
while(qf>qd&&que[qd].time<=k) qd++;
while(qf1>qd1&&que1[qd1].time<=k) qd1++;
mx=que[qd].key;
mi=que1[qd1].key;
}
}
}
printf("%d\n",ans); //单调队列,重要的是维护一个单调的队列,然后及时删除掉过时的元素。就可以保证在一个区间能找到最大值。
}
}
return ;
}

hdu4123(树形dp+单调队列)的更多相关文章

  1. (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  2. bzoj2500: 幸福的道路(树形dp+单调队列)

    好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...

  3. bzoj2500幸福的道路 树形dp+单调队列

    2500: 幸福的道路 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 434  Solved: 170[Submit][Status][Discuss ...

  4. Codeforces 980F Cactus to Tree 仙人掌 Tarjan 树形dp 单调队列

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF980F.html 题目传送门 - CF980F 题意 给定一个 $n$ 个节点 $m$ 条长为 $1$ 的边 ...

  5. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  6. POJ - 3162 Walking Race 树形dp 单调队列

    POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...

  7. [BZOJ 2500]幸福的道路 树形dp+单调队列+二分答案

    考试的时候打了个树链剖分,而且还审错题了,以为是每天找所有点的最长路,原来是每天起点的树上最长路径再搞事情.. 先用dfs处理出来每个节点以他为根的子树的最长链和次长链.(后面会用到) 然后用类似dp ...

  8. 【bzoj2500】幸福的道路 树形dp+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  9. 【POJ3162】Walking Race 树形dp+单调队列+双指针

    题目大意:给定一棵 N 个节点的无根树,边有边权,现生成一个序列 d,d[i] 表示 i 号节点到树上其他节点距离的最大值.给定一个 m,求 d 序列中最大值和最小值之差不超过 m 的最长连续段的长度 ...

随机推荐

  1. 构造函数、析构函数、赋值与初始化、explicit关键字

    一.构造函数.默认构造函数 (1).构造函数 构造函数是特殊的成员函数 创建类类型的新对象,系统自动会调用构造函数 构造函数是为了保证对象的每个数据成员都被正确初始化 函数名和类名完全相同 不能定义构 ...

  2. jenkins二

    破解管理员密码 1.假如我们忘记了Jenkins管理员密码了该怎么办呢?Jenkins没有用到数据库,所有的文件都是保存到xml文件里的 2.第一步找到admin所在的目录 [root@centos- ...

  3. Docker入门二

    容器管理 1.docker create创建一个容器,但容器并没启动,就和我们创建虚拟机一样,创建了虚拟机后没启动 [root@centos-02 ~]# docker create -it cent ...

  4. oracle和其他数据库对表名、列名的长度限制

    ============== 数据库 表名列名长度限制问题 今天修改数据库表名,感觉现有的定义列名都无含义...修改后被同事告知,列名有点长,怕有的数据库不支持.. 我头一次听说数据库表名和列名长度限 ...

  5. 如何在IIS7或IIS7.5中导入导出站点及应用程序池. -摘自网络

    为实现负载平衡,我们可能会使用多个WEB服务器,也就会需要给多个IIS配置同样的站点和应用程序池.那么我们需要一个一个的重新建吗?当然不用,我们只需要一些简单的命令就可以在IIS7(Windows S ...

  6. Ubuntu 16.04下搭建kubernetes集群环境

    简介 目前Kubernetes为Ubuntu提供的kube-up脚本,不支持15.10以及16.04这两个使用systemd作为init系统的版本. 这里详细介绍一下如何以非Docker方式在Ubun ...

  7. 【ecshop后台详解】系统设置-商店设置

    商店设置是我们ecshop新用户第一步先要设置的地方,因为里面相当于网站的基础.包括公司名称,电话,地址,tittle等重要的信息都是这里修改,如果这里没有修改的话,如果有访客来到你网站可能以为走错了 ...

  8. IP网络,光网络以及轨道交通的快速卸载随想

    凌晨3点钟,半夜睡眼朦胧.忽然听到左右两耳嗡嗡,身下的榻榻米垫沙沙作响,以为在梦境,然而睁眼清醒过来.发现并没有看见什么,依旧在黑夜,于是确认这不是在在梦.于是开灯,发现一仅仅蟑螂趴在垫子上.两仅仅蚊 ...

  9. 使用filter解决request.getParameter的中文乱码问题

    注意:一般一个站点的所有页面的编码,包括数据库编码都要保持一致,下面默认的编码都是UTF-8 ----------------------------------例1:直接提交到jsp页面------ ...

  10. Mac命令行启动MySQL

    #mysql 启动 mysql.server start #mysql停止 mysql.server stop #mysql重启 mysql.server restart