还没有学过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. 剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)

    一.移除性算法 (remove)  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  2. Python3中使用HTMLTestRunner报No module named 'StringIO'解决方法

    今天在学习使用HTMLTestRunner生成测试报告时遇到一个报错,如图所示: 网上搜索了下“No module named 'StringIO'”解决方法,原来我用的是Python 3.X版本,而 ...

  3. Struts2初学 Struts2的action接收用户数据方式

    一.简介    开发Web应用程序,首先应会遇到对用户输入数据的接收,传统的Web应用程序是由开发人员调用HttpServletRequest的getparameter(String name)方法从 ...

  4. 【转】用python实现简单的文本情感分析

    import jieba import numpy as np # 打开词典文件,返回列表 def open_dict(Dict='hahah',path = r'/Users/zhangzhengh ...

  5. mysql 循环insert

    亲测成功!可用,复制即可 DELIMITER ;; CREATE PROCEDURE test_insert() BEGIN DECLARE y TINYINT DEFAULT 1;WHILE y&l ...

  6. xmapp+netbeans调试

    1.netbeans的设置 2.修改php.ini 如下: [XDebug]zend_extension = "D:\dev\xampp\php\ext\php_xdebug.dll&quo ...

  7. [内核]Linux UserSpace和Kernel之间如何联系

    转自:http://blog.csdn.net/dreaming_my_dreams/article/details/8272586 应用层和驱动的衔接,一直是一个老大难问题,若弄不清楚,总觉得驱动写 ...

  8. 1.重学javascript (一)

    一.script标签解析 <script>xxx</script>这组标签,是用于在html 页面中插入js的主要方法.它主要有以下 几个属性: 1.charset:可选.表示 ...

  9. PCB标识说明

    VDC (Voltage Direct Current),直流电压 VBAT,电池供电 PWKEY,电源键 RXD,数据接收 TXD,数据发送 Receive Data ,Transmit Data ...

  10. C++ 运算符重载三(链式编程)

    //运算符重载之链式编程 #include<iostream> using namespace std; //对于友元函数重载运算符只适用于左操作数是系统变量的场景 //因为成员无法在系统 ...