题意:

给定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 树的直径+单调队列的更多相关文章

  1. 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 ...

  2. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  3. POJ 3162 Walking Race(树的直径+单调队列)

    题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的 ...

  4. 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 ...

  5. HDU 4123 Bob's Race:树的直径 + 单调队列 + st表

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...

  6. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  7. hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)

    C - Bob’s Race Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  8. HDU 4123(树的直径+单调队列)

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 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 ...

随机推荐

  1. 第1周 SQL Server 如何执行一个查询

    原文:第1周 SQL Server 如何执行一个查询 大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Se ...

  2. 【原创】leetCodeOj --- Factorial Trailing Zeroes 解题报告

    原题地址: https://oj.leetcode.com/problems/factorial-trailing-zeroes/ 题目内容: Given an integer n, return t ...

  3. as3文本框的动态拖拽和编辑

    如今非常多软件都支持了编辑界面的文本拖拽和点击编辑来直接改动数值, 这样便于操作, 并且体验性也好, 抛砖引玉吧 于是就用好久没编写的as3来写了一下: 由于用的flash ide写的没有提示, 就临 ...

  4. MVC模型与FishiGUI应用层MVC型号

    MVC概要: MVC (Modal View Controler)M是指数据模型,V是指用户界面,C则是控制器. 使用MVC的目的是将M和V的实现代码分离,从而使同一个程序能够使用不同的表现形式.比方 ...

  5. SharePoint 2010 BCS - 概要

    博客地址 http://blog.csdn.net/foxdave SharePoint 2010首次引入了BCS的概念 - Business Connectivity Service.即业务连接服务 ...

  6. Android架构分析之LOG模块

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 Andro ...

  7. zookeeper错误KeeperErrorCode = ConnectionLoss解决

    原因: 一般是由于连接还未完成就执行zookeeper的get/create/exsit操作引起的. 解决方法: 利用"CountDownLatch 类 + zookeeper的watche ...

  8. 【Jqurey EasyUI+Asp.net】---DataGrid增加、删、更改、搜

    在前面写了两,但不知道如何完成,对比刚刚开始学这个,他们摸着石头过河,一步步.在最后两天DataGridCRUD融合在一起.因此份额.我希望像我这样谁是刚刚开始学习一些帮助. 直接主题酒吧. 它是说数 ...

  9. 3、Spring4之Bean 配置的细节

    1). 若字面值中包括特殊字符,则能够使用 value 节点的 <![CDATA[]]> 把字面值包裹起来.      <constructor-arg>           ...

  10. rhel5.8 ISO yum源配置

    [root@lei1 mnt]# mkdir /mnt/iso [root@lei1 mnt]# mkdir /mnt/cdrom [root@lei1 ~]# mv rhel-server-5.8- ...