还没有学过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. js 获取中文的拼音首字母

    es6 + 模块化封装 "use strict"; module.exports = { //参数,中文字符串 //返回值:拼音首字母串数组 makePy (str) { if ( ...

  2. java后台分页实例一

    后台框架:jfinal + velocity.前台框架:jquery  页面 <!DOCTYPE html> <html> <head> <meta char ...

  3. 源码编译安装git

    debian上的git版本才2.1有点低了,为了安装最新版的2.11,我决定从源码编译安装一下. 预备工作: 1.安装编译工具.apt install -y  build-essential 2.安装 ...

  4. 对象.delegate=self的理解

    整理自:http://www.cocoachina.com/ask/questions/show/87430 各位大神,对象.delegate=self是啥意思,委托的意思不就是自己的任务交给其他人去 ...

  5. iconv 解决乱码问题

    [root@NGINX-APACHE-SVN pro]# file 林.txt 林.txt: ISO-8859 text, with no line terminators #在LINUX下显示乱码 ...

  6. python学习之yummain模块

    定义:`yum`的命令行接口. yummain.main(args) Run the yum program from a command line interface. yummain.hotsho ...

  7. [kernel]如何主动触发一次kernel panic

    Step1: echo 1 > /proc/sys/kernel/sysrq 或者如果不想每次运行上面的命令,可以echo "kernel.sysrq=1" >> ...

  8. [接口]mmc/eMMC/SD-card

    转自:http://blog.csdn.net/yazhouren/article/details/46643321 MMC(multiMedia card)是一种通信协议,支持两种模式SPI和MMC ...

  9. Android基础总结(八)Service

    服务两种启动方式(掌握) startService 开始服务,会使进程变成为服务进程 启动服务的activity和服务不再有一毛钱关系 bindService 绑定服务不会使进程变成服务进程 绑定服务 ...

  10. myeclipse工程重名后怎么更改deploy location?

    http://zhidao.baidu.com/link?url=I9E16OYfxovPHqBrRWhYCI9TYNG_X-Whg_X7QrJiOBXBGEwi-6WYsC-Zi4Jcg9zd3ye ...