还没有学过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. Centos 7 防火墙

    systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体.启动一个服务:systemctl start firewalld.service ...

  2. spring in action小结4.1

    1 横切关注点:可以被描述为影响应用多处的功能.横切关注点可以被模块化为特殊的类,这些类被称为切面. 2 AOP自己的术语,通知(Advice).切点(pointcut).连接点(joinpoint) ...

  3. atitit.提升兼容性最佳实践 o9o

    atitit.提升兼容性最佳实践 o9o.doc 1. Atitit.兼容性的"一加三"策略 1 2. 扩展表模式 1 3. 同时运行模式 1 3.1. 完美的后向兼容性 2 3. ...

  4. 5.1 Zend_Log_Writer

    22.2.2. 写入到数据库 22.2.3. 踩熄Writer 22.2.4. 測试 Mock 22.2.5. 组合Writers

  5. Elastic_Terms 内容分类统计

    Terms 按字段的值进行分类,并计算出doc_count, bucket聚合 类似于 group by 常用统计 分类并出现频率高的,并进一步挖出,计算出想要的数据. 参考资料 https://ww ...

  6. MongoDB随笔

    创建用户 db.createUser({user: "abc",pwd: "abc123",roles: [ { role: "readWrite&q ...

  7. C语言 · 约数个数

    算法提高 约数个数   时间限制:1.0s   内存限制:512.0MB      输入一个正整数N,输出其约数的个数. 样例输入 12 样例输出 6 样例说明 12的约数包括:1,2,3,4,6,1 ...

  8. 16C554在LINUX上的移植(AT91)

    16C554在LINUX上的移植(AT91) linux版本:3.14.17 AT91SAMa5d36   EINTA_0   ARM-IO5        PA14         14 EINTA ...

  9. 递归删除子目录下所有.la后缀文件

    删除当前目录及其子目录下的后缀名.la的所有文件 find ./ -name '*.la' | xargs rm -f

  10. kickstart安装步骤

    1.1 环境说明 [root@test ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@test ~]# uname -r 2 ...