C - Bob’s Race

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
 
题意: 给一颗无向有权树,选择一些连续的点作为起点,每个起点u有一个人跑步要尽可能的跑得远,用ai表示第i个点作为起点能跑得最远的距离,起点选择l...r这些点,那么对每个询问求一对l和r,满足max(al...ar) - min(al...ar) <= Q, 要求l和r的差值尽量大,输出这个最大差值
思路: 首先dfs+dp求每个点u能跑的最远距离,然后对每个询问用一个双指针扫一遍过去,复杂度o(n)
 
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = ; struct _edge{
int to,w,next;
};
_edge edge[N<<];
int ecnt,head[N];
inline void addedge(int u,int v,int w)
{
edge[ecnt].to = v;
edge[ecnt].w = w;
edge[ecnt].next = head[u];
head[u]=ecnt++;
} int n,m,a[N];
int dp[N][],id[N][]; void dfs1(int u,int fa)
{
dp[u][]=dp[u][]=;
for(int e=head[u];e!=-;e=edge[e].next)
{
int &v = edge[e].to;
if(v==fa) continue;
dfs1(v,u);
int t1 = dp[v][] + edge[e].w, t2 = v;
if(t1>=dp[u][])
{
swap(t1,dp[u][]);
swap(t2,id[u][]);
}
if(t1>=dp[u][])
{
swap(t1,dp[u][]);
swap(t2,id[u][]);
}
}
} void dfs2(int u,int fa,int up)
{
a[u]=max(dp[u][],up);
for(int e=head[u];e!=-;e=edge[e].next)
{
int &v = edge[e].to;
if(v==fa) continue;
int t;
if(v==id[u][])
t = max(dp[u][],up);
else
t = max(dp[u][],up);
dfs2(v,u,t+edge[e].w);
}
} int dmx[N][],dmn[N][];
void RMQ_init()
{
for(int i=;i<=n;i++) dmn[i][]=dmx[i][]=a[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dmn[i][j]=min(dmn[i][j-],dmn[i+(<<(j-))][j-]),
dmx[i][j]=max(dmx[i][j-],dmx[i+(<<(j-))][j-]);
}
void query(int l,int r,int &mn,int &mx)
{
int k = ;
while((<<(k+))<=r-l+) k++;
mn = min(dmn[l][k],dmn[r-(<<k)+][k]);
mx = max(dmx[l][k],dmx[r-(<<k)+][k]);
} void run()
{
int u,v,w;
ecnt=;
memset(head,-,sizeof(head));
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
memset(dp,-,sizeof(dp));
memset(id,-,sizeof(id));
dfs1(,-);
dfs2(,-,);
// for(int i=1;i<=n;i++)
// cout<<a[i]<<' ';
// cout<<endl; RMQ_init(); int q;
while(m--)
{
scanf("%d",&q);
int ans = ;
int l=,r;
int mn,mx;
mn=mx=a[];
for(r=;r<=n;r++)
{
if(a[r]>mx) mx=a[r];
if(a[r]<mn) mn=a[r];
while(mx-mn>q)
{
l++;
query(l,r,mn,mx);
}
if(ans < r-l+)
ans = r-l+;
}
printf("%d\n",ans);
}
} int main()
{
// freopen("in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF && n)
run();
return ;
}
 
 
 

hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)的更多相关文章

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

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

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

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

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

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

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

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

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

  7. HDU 4123 Bob’s Race 树的直径+单调队列

    题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...

  8. HDU 4123 Bob’s Race(RMQ)

    题意是说给出一棵树,N(10^5)个顶点,以及每条边的权值,现在需要选择连续的K个点(顶点编号连续),可以被选出来的条件是: 若d[i]代表顶点i到树上其他点的距离的最大值,使得区间[a, b]的d值 ...

  9. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

随机推荐

  1. 【oracle案例】ORA-01102: cannot mount database in EXCLUSIVE mode

    ORA-01102: cannot mount database in EXCLUSIVE mode 今天在fedora上安装完10g后,测试数据库是否安装成功.STARTUP数据库时,发生如下错误: ...

  2. linux c编程:进程间通信

    进程间的通信包括管道,共享内存,信号量通信,消息队列,套借口(socket)和全双工管道通信 首先来看下管道的用法:管道顾名思义,就如同下水道管道一样,当从管道一端流水到另一端的时候,水流的方向是单方 ...

  3. Nodejs 中常见的加密算法:RSA(1)

    Linux用户(以Ubuntu为例) $ openssl 进入OpenSSL程序 OpenSSL> genrsa -out rsa_private_key.pem 1024 生成私钥 OpenS ...

  4. Nginx + Tomcat 应用证书启用 SSL

    第一部分 简述 - 附:相关概念 1 Nginx 是什么? - 2 Tomcat 是什么? - 3 SSL 是什么? Secure Sockets Layer,现在应该叫"TLS" ...

  5. 用JavaScript判断一个对象是否数组?

    Q:如何判断一个对象是否为数组? A1:判断对象的constructor是否指向Array, 接着判断对应的特殊属性,如length,splice之类.这个很容易冒充. A2:使用instanceof ...

  6. selector + drawable 多状态图形

    select_drawble.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...

  7. HDU - 3068 最长回文 【Manacher】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3068 思路 n^3 的做法 对于每个字符 同时 往左往右搜 但是要分奇偶 就是 n^3 n^2 的做法 ...

  8. video codec 学习笔记

    一. H.264 (http://www.baike.com/wiki/H264)  三大标准: AVC(Advanced Video Coding,AVC) H.264,同时也是MPEG-4第十部分 ...

  9. 细说后端模板渲染、客户端渲染、node 中间层、服务器端渲染(ssr)

    细说后端模板渲染.客户端渲染.node 中间层.服务器端渲染(ssr) 前端与后端渲染方式的发展大致经历了这样几个阶段:后端模板渲染.客户端渲染.node 中间层.服务器端渲染(ssr). 1. 后端 ...

  10. Ubuntu编译Android使用的FFmpeg

    本文介绍在Ubuntu平台编译FFmpeg库,用于Android使用.前提需要配置好NDK的环境.可以参考之前的文章Android NDK环境搭建. 下载FFmpeg 在官网下载FFmpeg源码,ht ...