Walking Race
Time Limit: 10000MS   Memory Limit: 131072K
Total Submissions: 4123   Accepted: 1029
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 ≤ 106) and M (M < 109). 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

Hint

Explanation for the sample:

There are three check-points. Two paths of lengths 1 and 3 connect check-points 2 and 3 to check-point 1. The three paths along with wc walks are 1-3, 2-1-3 and 3-1-2. And their lengths are 3, 4 and 4. Therefore data from all three days can be used for analysis.

Source

 
题意:有一棵n个节点的树,每天从i点开始跑,跑最远能到达的距离,求一段连续的天数,这些天里面跑的最多的和最少的距离相差不能超过m,求最多的连续天数。
思路:树形dp。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=1e6+,M=2e6+;
const int inf=0x3f3f3f3f;
const ll INF=1e13+,mod=1e9+;
struct edge
{
int from,to;
ll w;
int next;
};
edge es[M];
int cnt,head[N];
ll dp[N][];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,ll w)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].w=w;
es[cnt].next=head[u];
head[u]=cnt;
}
void dfs1(int u,int fa)
{
dp[u][]=dp[u][]=;
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa) continue;
dfs1(e.to,u);
ll tmp=dp[e.to][]+e.w;
if(tmp>dp[u][]) swap(tmp,dp[u][]);
if(tmp>dp[u][]) swap(tmp,dp[u][]);
}
}
void dfs2(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa) continue;
if(dp[e.to][]+e.w==dp[u][])
dp[e.to][]=max(dp[u][],dp[u][])+e.w;
else dp[e.to][]=max(dp[u][],dp[u][])+e.w;
dfs2(e.to,u);
}
}
ll Tree1[N<<],Tree2[N<<];
void pushup(int pos)
{
Tree1[pos]=max(Tree1[pos<<],Tree1[pos<<|]);
Tree2[pos]=min(Tree2[pos<<],Tree2[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
Tree1[pos]=Tree2[pos]=max(dp[l][],dp[l][]);
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void query(ll &ans1,ll &ans2,int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
ans1=max(ans1,Tree1[pos]);
ans2=min(ans2,Tree2[pos]);
return;
}
int mid=(l+r)>>;
if(L<=mid) query(ans1,ans2,L,R,l,mid,pos<<);
if(R>mid) query(ans1,ans2,L,R,mid+,r,pos<<|);
}
int main()
{
int n;
ll m;
scanf("%d%lld",&n,&m);
init();
for(int i=,j; i<=n; i++)
{
ll w;
scanf("%d%lld",&j,&w);
addedge(i,j,w),addedge(j,i,w);
}
memset(dp,,sizeof(dp));
dfs1(,);
dfs2(,);
int ans=;
build(,n,);
for(int i=,j=; i<=n&&j<=n;)
{
ll maxx=-INF,minx=INF;
query(maxx,minx,i,j,,n,);
if(maxx-minx<=m) ans=max(ans,j-i+),j++;
else i++;
if(n-i<ans) break;
}
printf("%d\n",ans);
return ;
}

树形dp

POJ 3162.Walking Race 树形dp 树的直径的更多相关文章

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

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

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

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

  3. 【题解】poj 3162 Walking Race 树形dp

    题目描述 Walking RaceTime Limit: 10000MS Memory Limit: 131072KTotal Submissions: 4941 Accepted: 1252Case ...

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

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

  5. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

  6. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 4607 树形dp 树的直径

    题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...

  8. computer(树形dp || 树的直径)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. VIJOS1476旅游规划[树形DP 树的直径]

    描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...

随机推荐

  1. 【Python学习】Python3 环境搭建

    参考地址:http://www.runoob.com/python3/python3-install.html Python3 环境搭建 本章节我们将向大家介绍如何在本地搭建 Python3 开发环境 ...

  2. Javascript FormData实例

    一.创建一个formData对象实例的方式 1.创建一个空对象 var formData = new FormData();//通过append方法添加数据 1 2.使用已有表单来初始化对象 //表单 ...

  3. 关于JWTtoken的管理问题

    JWT简介: Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准.因为网络上有很多关于jwt的详细介绍了,所以我这里就不再赘述.但是JWT的大 ...

  4. Shell 批量修改主机 用户密码

    问题:132.121.114 和 132.121.118 网段共 48 台主机未添加基础监控,但是 wh 账户不能登录 需进行批量修改密码操作. 目前情况:op1对上述48台机器设备均能免密登录. 操 ...

  5. 用es5原生模仿-es6Promise异步处理

    用es5原生模仿-es6Promise异步处理,不过在处理异常的时候有点小bug不是很完美,不过多级then 是没问题的和resolve, rejec  正常调用和异常处理调用是没问题的.本帖属于原创 ...

  6. 循环队列搜索 Search in Rotated Sorted Array

    这里比较重要的是,不要一上来就判断mid 和 target有没有关系.因为数组是无序的,这样的判断毫无结论,只会搞的更复杂.应该先想办法判断出哪一侧是有序的. class Solution { pub ...

  7. Hadoop Mapreduce的shuffle过程详解

    1.map task读取数据时默认调用TextInputFormat的成员RecoreReader,RecoreReader调用自己的read()方法,进行逐行读取,返回一个key.value; 2. ...

  8. jQuery之位置坐标图形相关方法

    jQuery实例方法-位置图形 位置坐标图形大小相关方法: .offset() .position() .scrollTop() ..scrollLeft() .width()..height() . ...

  9. ES6-你不知道的箭头函数

    一谈到ES6的箭头函数,大家可能想到的优点就是语法更简洁,因为去掉了return.function.{}等输入. 但是设计者果真就是出于简洁的目的推出的箭头函数吗?显然不是.   => 箭头函数 ...

  10. docker搭建gitlab服务器(Centos7)

    系统环境:CentOS Linux release 7.6.1810 (Core) git版本:gitlab/gitlab-ce 一.安装和启动docker 见HTTPRUNNERMANAGER安装部 ...