题目描述

Walking Race
Time Limit: 10000MS Memory Limit: 131072K
Total Submissions: 4941 Accepted: 1252
Case Time Limit: 3000MS

Description

flymouse’s sister wc is very capable at sports and her favorite event is walking race. Chasing after the championship in an important competition, she comes to a training center to attend a training course. The center has N check-points numbered 1 through N. Some pairs of check-points are directly connected by two-way paths. The check-points and the paths form exactly a tree-like structure. The course lasts N days. On the i-th day, wc picks check-point i as the starting point and chooses another check-point as the finishing point and walks along the only simple path between the two points for the day’s training. Her choice of finishing point will make it that the resulting path will be the longest among those of all possible choices.

After every day’s training, flymouse will do a physical examination from which data will obtained and analyzed to help wc’s future training be better instructed. In order to make the results reliable, flymouse is not using data all from N days for analysis. flymouse’s model for analysis requires data from a series of consecutive days during which the difference between the longest and the shortest distances wc walks cannot exceed a bound M. The longer the series is, the more accurate the results are. flymouse wants to know the number of days in such a longest series. Can you do the job for him?

Input

The input contains a single test case. The test case starts with a line containing the integers N (N ≤ 10^{6}) and M (M < \10^{9}). Then follow N − 1 lines, each containing two integers fi and di (i \= 1, 2, …, N − 1), meaning the check-points i + 1 and fi are connected by a path of length di.

Output

Output one line with only the desired number of days in the longest series.

Sample Input

3 2
1 1
1 3

Sample Output

3

题目大意

题目大概是给一棵n个结点边带权的树,记结点i到其他结点最远距离为d[i],问d数组构成的这个序列中满足其中最大值与最小值的差不超过m的连续子序列最长是多长。

思路

各个结点到其他结点的最远距离可以用树形DP解决,HDU2196

因为有最大值和最小值,需要两个单调队列,一个维护最大值qmax,另一个维护最小值qmin

具体操作看代码

代码

#include<cmath>
#include<deque>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
const int MAXN=1e6+200;
struct edge {
int to,next,w;
} edges[MAXN*2];
int tot,dis[MAXN];
int head[MAXN];
void add_edge(int u,int v,int w) {
edges[tot].to=v;
edges[tot].w=w;
edges[tot].next = head[u];
head[u] = tot++;
}
int dist[MAXN][3]; //dist[i][0,1,2]分别为正向最大距离,正向次大距离,反向最大距离
int longest[MAXN];
int dfs1(int u,int fa) {
if(dist[u][0]>=0) return dist[u][0];
dist[u][0]=dist[u][1]=dist[u][2]=longest[u]=0;
for(re i=head[u];i!=-1;i=edges[i].next) {
int v=edges[i].to;
if(v==fa) continue;
if(dist[u][0]<dfs1(v,u)+edges[i].w) {
longest[u]=v;
dist[u][1]=max(dist[u][1],dist[u][0]);
dist[u][0]=dfs1(v,u)+edges[i].w;
}
else if(dist[u][1]<dfs1(v,u)+edges[i].w)
dist[u][1]=max(dist[u][1],dfs1(v,u)+edges[i].w);
}
return dist[u][0];
} void dfs2(int u,int fa) {
for(int i=head[u];i!=-1;i=edges[i].next) {
int v = edges[i].to;
if(v==fa)continue;
if(v==longest[u]) dist[v][2]=max(dist[u][2],dist[u][1])+edges[i].w;
else dist[v][2]=max(dist[u][2],dist[u][0])+edges[i].w;
dfs2(v,u);
}
} int finish(int n,int M){
if(n <= 0) return n;
if(M < 0) return 0;
deque<int> qmax,qmin; //qmax单调递减 qmin单调递增
deque<int> idmax,idmin; //id存节点编号(同样单调)
int ans=0;
int left=1,right=1;
while(right <= n){
//维护单调性
while(!qmax.empty() && dis[right] >= qmax.back()) qmax.pop_back(), idmax.pop_back();
qmax.push_back(dis[right]); idmax.push_back(right);
while(!qmin.empty() && dis[right] <= qmin.back()) qmin.pop_back(), idmin.pop_back();
qmin.push_back(dis[right]); idmin.push_back(right);
while(qmax.front()-qmin.front() > M && left<right){ //超出m就减少最大值/增大最小值(左指针右移)
left++;
while(idmax.front() < left) idmax.pop_front(), qmax.pop_front();
while(idmin.front() < left) idmin.pop_front(), qmin.pop_front();
}
ans = max(ans,right-left+1);
right++;
}
return ans;
}
int main() {
int n,m;
while(scanf("%d%d",&n,&m)==2&&n) {
tot=0;
memset(dist,-1,sizeof(dist));
memset(head,-1,sizeof(head));
memset(longest,-1,sizeof(longest));
for(int i=2; i<=n; i++) {
int v,w;
scanf("%d%d",&v,&w);
add_edge(i,v,w);
add_edge(v,i,w);
}
dfs1(1,-1);
dfs2(1,-1);
for(int i=1;i<=n;i++) dis[i]=max(dist[i][0],dist[i][2]);
printf("%d\n",finish(n,m));
}
return 0;
}

【题解】poj 3162 Walking Race 树形dp的更多相关文章

  1. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  2. POJ - 3162 Walking Race 树形dp 单调队列

    POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...

  3. POJ 3162 Walking Race 树形DP+线段树

    给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...

  4. POJ 3162 Walking Race 树形dp 优先队列

    http://poj.org/problem?id=3162 题意 :  一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...

  5. POJ 3162 Walking Race(树形dp+单调队列 or 线段树)

    http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...

  6. 【POJ3162】Walking Race 树形dp+单调队列+双指针

    题目大意:给定一棵 N 个节点的无根树,边有边权,现生成一个序列 d,d[i] 表示 i 号节点到树上其他节点距离的最大值.给定一个 m,求 d 序列中最大值和最小值之差不超过 m 的最长连续段的长度 ...

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

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

  8. POJ 3162 Walking Race (树的直径,单调队列)

    题意:给定一棵带边权的n个节点的树,首先要求出每个点的最长路,然后写成序列d[1],d[2]...d[n],然后求满足 区间最大值-区间最小值<=k 的最大区间长度为多少? 思路: 分两步进行: ...

  9. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

随机推荐

  1. 面试题---->线程的入门,读完可以应付一般的面试(管理员不要移除我的随笔啊)

    这个都是入门和一般的常规知识,大佬轻喷 ①.继承Thread类 ②.实现Runnable接口(常用,优点多) ③.实现Callable接口 实现Runnable和Callable接口的类只能当作一个可 ...

  2. 手写一个最简单的IOC容器,从而了解spring的核心原理

    从事开发工作多年,spring源码没有特意去看过.但是相关技术原理倒是背了不少,毕竟面试的那关还是得过啊! 正所谓面试造火箭,工作拧螺丝.下面实现一个最简单的ioc容器,供大家参考. 1.最终结果 2 ...

  3. 看雪加密解密第一个traceme程序破解

    工具:ollydbg(吾爱破解2.10版) 工具设置:因为traceme是一个win32图形用户程序,所以其程序入口点在WinMain()函数处,设置ollydbg的调试设置的事件选项,选中在WinM ...

  4. wrk 及扩展支持 tcp 字节流协议压测

    wrk 及扩展支持 tcp 字节流协议压测 高性能.方便使用的 HTTP(s) 的流量压测工具,结合了多个开源项目开发而成: redis 的 ae 事件框架 luajit openssl http-p ...

  5. 深入探索Android热修复技术原理读书笔记 —— so库热修复技术

    热修复系列文章: 深入探索Android热修复技术原理读书笔记 -- 热修复技术介绍 深入探索Android热修复技术原理读书笔记 -- 代码热修复技术 深入探索Android热修复技术原理读书笔记 ...

  6. Java 正则表达式实例操作

    Regular Expression正则表达式,简称RegExp,常规通用的表达式,在多个开发语言中都有它的实现,可以通过正则表达式来快速的检索.匹配.查找.替换字符串中的文本. 简单实例 匹配网址 ...

  7. 如何安装多个jdk并方便切换系统jdk版本

    如何安装多个jdk并方便切换系统jdk版本 前言 在安装myeclipse时,压缩包中附带1.8.0的jdk,顺便安装并配置环境变量后发现系统默认的jdk变为了1.8.0.随后发现eclipse只支持 ...

  8. Linux 实验楼

    网络上的免费在线 Linux 实验系统 Wu Zhangjin 创作于 2014/01/12 打赏 by falcon of TinyLab.org 2014/01/12 这里收集各类可以直接在线访问 ...

  9. CentOS 8.2远程连接vncserver升级后1.10.1无法启动解决记录

    CentOS 8.2远程连接vncserver升级后1.10.1无法启动解决记录   问题起源:手贱yum upgrade,重启服务器后无法使用vnc viewer远程连接 查看状态 # system ...

  10. 1.5 RPM红帽软件包1.6 Yum软件仓库

    1.5 RPM红帽软件包 在RPM(红帽软件包管理器)公布之前,要想在Linux系统中安装软件只能采取源码包的方式安装.早期在Linux系统中安装程序是一件非常困难.耗费耐心的事情,而且大多数的服务程 ...