HDU 4123 Bob’s Race 树的直径+单调队列
题意:
给定n个点的带边权树Q个询问。
以下n-1行给出树
以下Q行每行一个数字表示询问。
首先求出dp[N] :dp[i]表示i点距离树上最远点的距离
询问u, 表示求出 dp 数组中最长的连续序列使得序列中最大值-最小值 <= u,输出这个序列的长度。
思路:
求dp数组就是求个树的直径然后dfs一下。
对于每一个询问,能够用一个单调队列维护一下。O(n)的回答。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if(x>9) pt(x/10);
putchar(x%10+'0');
}
typedef long long ll;
const int N = 50010;
int n, Q;
struct Edge{
int to, nex; ll dis;
}edge[N<<1];
struct node {
int v, id;
node() {}
node(int _id, int _v) {
id = _id; v = _v;
}
};
int head[N], edgenum;
void init(){for(int i = 1; i <= n; i++)head[i] = -1; edgenum = 0;}
void add(int u, int v, ll d){
Edge E = {v, head[u], d};
edge[edgenum] = E;
head[u] = edgenum++;
}
ll dis[N], dp[N], len;
int Stack[N], top, pre[N], vis[N];
int BFS(int x){
for(int i = 1; i <= n; i++)
dis[i] = -1;
dis[x] = 0; pre[x] = -1;
int far = x;
queue<int> q; q.push(x);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].nex){
int v = edge[i].to;
if(dis[v] == -1)
{
dis[v] = dis[u] + edge[i].dis;
pre[v] = u;
if(dis[far] < dis[v])
far = v;
q.push(v);
}
}
}
return far;
}
void dfs(int u){
vis[u] = 1;
for(int i = head[u]; ~i; i = edge[i].nex)
{
int v = edge[i].to;
if(vis[v])continue;
dp[v] = dp[u] + edge[i].dis;
dfs(v);
}
}
void build(){//预处理树的直径
int E = BFS(1);
int S = BFS(E);
top = 0;
int u = S;
len = dis[S];
for(int i = 1; i <= n; i++) vis[i] = 0;
while(u!=-1)
{
Stack[top++] = u;
dp[u] = max(dis[u], len - dis[u]);
vis[u] = 1;
u = pre[u];
}
for(int i = 0; i < top; i++) dfs(Stack[i]);
}
void input(){
init(); ll d;
for(int i = 1, u, v; i < n; i++)
{
rd(u); rd(v); rd(d);
add(u, v, d); add(v, u, d);
}
} node mx[N], mi[N];
int h1, t1, h2, t2;
int main() {
int v, idx, ans;
while(cin>>n>>Q, n+Q) {
input();
build();
while(Q--)
{
rd(v);
ans = h1 = t1 = h2 = t2 = 0;
idx = 1;
for (int i = 1; i <= n; ++i) {
while (h1!=t1 && mx[t1-1].v <= dp[i])
-- t1;
mx[t1++] = node(i, dp[i]);
while (h2!=t2 && mi[t2-1].v >= dp[i])
-- t2;
mi[t2++] = node(i, dp[i]);
while (h1!=t1&&h2!=t2) {
if (mx[h1].v-mi[h2].v>v)
++ idx;
else
break;
while (h1!=t1&&mx[h1].id<idx)
++h1;
while (h2!=t2&&mi[h2].id<idx)
++h2;
}
ans = max(ans, i-idx+1);
}
pt(ans);
putchar('\n');
}
}
return 0;
}
HDU 4123 Bob’s Race 树的直径+单调队列的更多相关文章
- HDU 4123 Bob’s Race 树的直径 RMQ
Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- POJ 3162 Walking Race(树的直径+单调队列)
题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的 ...
- HDU 4123 Bob’s Race 树的直径+ST表
Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...
- HDU 4123 Bob's Race:树的直径 + 单调队列 + st表
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...
- HDU 4123 Bob’s Race 树形dp+单调队列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...
- hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)
C - Bob’s Race Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4123 Bob’s Race(树形DP,rmq)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- WITH AS
表 id pid name获取 下面所有的子节点
- asp.net不能调试,配置一切正常
Asp.net发展中遇到的一个奇怪的想象:一个简单的button事件,不能调试.即使webconfig里面 "debug=true". 开发环境:win7+VS2005+IE8. ...
- ORA-00600 [kollasg:client-side tmp lob]
今天在查看一个库的日志时,发现被ORA-00600 [kollasg:client-side tmp lob] 错误刷屏了. 发生该错误的原因是由于应用那边lob的问题.lob没有被初始化,建议使用E ...
- [原创] 使用rpi + crontab + git 定时向bitbucket 推送 照片
#前提条件,你得有一个bitbucket的帐户 1.定时启动脚本代码 使用的是crontab Cronfile 内容如下: 此文件的意思是,每隔10分钟,就运行一次 /home/pi/Rpi_uplo ...
- 客户端上显示csdn上的各类别下的的文章列表 (制作csdn app 三)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/23597229 今天将在Android 使用Fragment,ViewPagerI ...
- Duanxx的STM32学习: 启动模式,BOOT0和BOOT1具体解释
在画STM32的电路图的时候,关于STM32的启动方式纠结了一下,现有的參考设计都是在STM32的启动选择引脚BOOT0和BOOT1上使用了跳帽,用以人工选择STM32的启动方式,可是在实际应用中这样 ...
- c++爱问的面试问题
1.static_cast,dynamic_cast,reinterpret_cast,const_cast四种转换. 2.const行为 3.malloc/free, new/delete差额 4. ...
- OutputCache说明
当用户访问该页面,整个页面会server存储在内存中,因此,该页面缓存.当用户再次访问该页面,页面不会再次运行数据操作,页面首先检查server中是否存在缓存.假设缓存存在,则直接从缓存中获取页面信息 ...
- Android - 数据存储 -在SQL数据库中保存数据
对于重复的或结构化的数据,保存到数据库中是很好的选择,比如联系人信息.这里假设你对SQL数据库大体上了解然后帮助你学习Android上的SQLite数据库.在Android数据库上需要用到的API可以 ...
- ym——Android之ListView性能优化
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! Android之ListView性能优化 假设有看过我写过的15k面试题的朋友们一定知 ...