还没有学过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. SQL SERVER 2005允许自定义聚合函数

    不多说了,说明后面是完整的代码,用来将字符串型的字段的各行的值拼成一个大字符串,也就是通常所说的Concat 例如有如下表dict  ID  NAME  CATEGORY  1 RED  COLOR  ...

  2. mysql 插入 详解

    表创建好后,就可以往里插入记录了,插入记录的基本语法如下: INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,…… ...

  3. hibernate中一些属性对操作的影响

    1 inverse,在一对多中使用,表示是否有关联关系控制权.对于保存.删除数据有影响. 2 cascade,表示级联操作 save-update 表示级联保存和更新 delete 表示级联删除 al ...

  4. unity, EventType.MouseUp注意事项

    如果鼠标移出了窗口范围,则即使鼠标抬起也不会收到EventType.MouseUp消息,所以只写 if(event==EventType.MouseUp){ 执行某操作 } 是错误的,会导致非常奇怪的 ...

  5. [elk]elasticsearch dashboard+保留10天内索引+导入导出备份

    es dashboard 有两款 head 这款我一直在用 https://github.com/mobz/elasticsearch-head 先修改es的配置文件: elasticsearch.y ...

  6. tornado部署

    1.为什么要运行多个tornado实例同步请求时,在应用处理过程中(如数据库查询,磁盘访问),服务器进程不能接受新请求.所以需要运行多个服务器进程实例.异步请求时,在应用处理时,服务器进程是非阻塞的, ...

  7. 如何重设 MySQL 的 root 密码

    MySQL下创建新用户.新数据库.设定访问权限控制都需要用到root密码.万一把root密码忘了,该怎么办? 幸运地是,重设密码很容易. 安全模式重置法 基本的思路是,以安全模式启动mysql,这样不 ...

  8. Spring--初始化IOC容器的几种方式

    初始化beanfactory主要有以下的三种方式:    1.filesystemXml Resource resource = new FileSystemResource("beans. ...

  9. sqlmap中tamper脚本绕过waf

    0x00 背景 sqlmap中的tamper脚本来对目标进行更高效的攻击. 由于乌云知识库少了sqlmap-tamper 收集一下,方便学习. 根据sqlmap中的tamper脚本可以学习过绕过一些技 ...

  10. C++ 友元类,友元函数

    //友元函数 友元类 #include<iostream> using namespace std; class PointB { public: friend class PointC; ...