Walking Race
 

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.

【题意】

  对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到d[1],d[2],d[3]……d[n] ,在数列的d中求一个最长的区间,使得区间中的最大值与最小值的差不超过m。

【分析】

  实际上这是一道很水的题目。

  这里涉及到一个 树的直径问题,树的直径就是树上距离最远的一条路径。而从一个点出发的最长路径,一定是以直径的一个端点为终点的。

  不过不知道也没所谓,反正两个dfs也可以求最长路径啦。

  后面就是一个序列求最长区间,使max-min<=m的问题了。

  设l[i]为以i为末尾的最长合法区间长度,我们知道这个是有一定单调性的,即l[i]<=l[i-1]+1,所以我们弄个指针不断右移就好了。

  现在问题变成判断这个区间是否合法,那就是要求出max[a...b]和min[a...b],b不断递增,max和min都有单调性,可以用单调队列维护。

  时间复杂度是O(n)。

  我一开始用RMQ求最值MLE了,听说用线段树求最值可以过~~

RMQ的MLE代码如下:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 1000001 struct node
{
int x,y,c,next;
}t[*Maxn];int len;
int first[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} int n,m;
int f[Maxn],g[Maxn],d[Maxn],v[Maxn];
void dfs(int x,int fa)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
v[y]=t[i].c;
dfs(y,x);
f[x]=mymax(f[x],f[y]+t[i].c);
}
} void dfs2(int x,int fa)
{
int st=-,now=-;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
if(st==-||f[y]+v[y]>f[st]+v[st]) st=y;
}
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa&&t[i].y!=st)
{
int y=t[i].y;
if(now==-||f[now]+v[now]<f[y]+v[y]) now=y;
g[y]=mymax(f[st]+v[st],g[x])+t[i].c;
dfs2(y,x);
}
if(st!=-)
{
g[st]=mymax(g[x],f[now]+v[now])+v[st];
dfs2(st,x);
}
} int mx[Maxn][],mn[Maxn][]; void rmq_init()
{
for(int i=;i<=n;i++) mx[i][]=mn[i][]=d[i];
for(int i=n;i>=;i--)
for(int j=;i+(<<j)-<=n;j++)
{
mx[i][j]=mymax(mx[i][j-],mx[i+(<<j-)][j-]);
mn[i][j]=mymin(mn[i][j-],mn[i+(<<j-)][j-]);
}
} int get_ans(int l,int r)
{
int st=;
while(l+(<<st)<=r) st++;
st--;
return mymax(mx[l][st],mx[r-(<<st)+][st])-mymin(mn[l][st],mn[r-(<<st)+][st]);
} int main()
{
scanf("%d%d",&n,&m);
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(i,x,y);ins(x,i,y);
}
memset(f,,sizeof(f));
memset(g,,sizeof(g));
memset(d,,sizeof(d));
dfs(,);
dfs2(,);
for(int i=;i<=n;i++) d[i]=mymax(f[i],g[i]);
memset(mx,,sizeof(mx));
memset(mn,,sizeof(mn));
rmq_init();
int now=,ans=;
for(int i=;i<=n;i++)
{
while(get_ans(now,i)>m&&now<i) now++;
ans=mymax(ans,i-now+);
}
printf("%d\n",ans);
return ;
}

[POJ 3162]

单调队列AC代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 1000100 struct node
{
int x,y,c,next;
}t[*Maxn];int len;
int first[Maxn]; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} int n,m;
int f[Maxn],g[Maxn],d[Maxn],v[Maxn];
void dfs(int x,int fa)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
v[y]=t[i].c;
dfs(y,x);
f[x]=mymax(f[x],f[y]+t[i].c);
}
} void dfs2(int x,int fa)
{
int st=-,now=-;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
if(st==-||f[y]+v[y]>f[st]+v[st]) st=y;
}
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa&&t[i].y!=st)
{
int y=t[i].y;
if(now==-||f[now]+v[now]<f[y]+v[y]) now=y;
g[y]=mymax(f[st]+v[st],g[x])+t[i].c;
dfs2(y,x);
}
if(st!=-)
{
g[st]=mymax(g[x],f[now]+v[now])+v[st];
dfs2(st,x);
}
} int mx[Maxn],mn[Maxn],edx[Maxn],edn[Maxn];
int sx=,sn=; void add(int x,int y)
{
while(mx[sx]<=y&&sx>) sx--;
mx[++sx]=y;edx[sx]=x;
while(mn[sn]>=y&&sn>) sn--;
mn[++sn]=y;edn[sn]=x;
} int ax=,an=;
int get_ans(int x)
{
if(ax>sx) ax=sx;
while(x>edx[ax]) ax++;
if(an>sn) an=sn;
while(x>edn[an]) an++;
return mx[ax]-mn[an];
} int main()
{
scanf("%d%d",&n,&m);
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(i,x,y);ins(x,i,y);
}
memset(f,,sizeof(f));
memset(g,,sizeof(g));
memset(d,,sizeof(d));
dfs(,);
dfs2(,);
for(int i=;i<=n;i++) d[i]=mymax(f[i],g[i]);
memset(mx,,sizeof(mx));
memset(mn,,sizeof(mn));
int now=,ans=;
for(int i=;i<=n;i++)
{
add(i,d[i]);
while(get_ans(now)>m&&now<i) now++;
ans=mymax(ans,i-now+);
}
printf("%d\n",ans);
return ;
}

[POJ 3162]

2016-10-17 14:03:00

【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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. 2019CCPC-江西省赛 -A Cotree (树形DP,求树上一点到其他点的距离之和)

    我是傻逼我是傻逼 #include<bits/stdc++.h> using namespace std; const int maxn=4e5+50; typedef long long ...

随机推荐

  1. ASP.NET Mvc开发之EF延迟加载

    EF延迟加载:就是使用Lamabda表达式或者Linq 从 EF实体对象中查询数据时,EF并不是直接将数据查询出来,而是在用到具体数据的时候才会加载到内存. 一,实体对象的Where方法返回一个什么对 ...

  2. 定时执行Timer

    JAVA import java.awt.event.*; import java.io.BufferedWriter;import java.io.File;import java.io.FileO ...

  3. HTML DOM对象

    HTML DOM对象 Document对象每个载入浏览器的HTML文档都会成为Document对象Document对象让我们可以从javascript中操作文档中的所有元素Document对象是win ...

  4. react 学习之十月之思

    学习新技术,最怕的莫过于自己抱着莫大的决心去学习,然发现没有学到东西,这是很可怕的事情,但是能坚持下去,一点一点的消化知识点,并且去理解它是什么?有什么用?该怎么去用?使用的时候需要注意些什么呢? 这 ...

  5. A swift Tour

    传统的认为,一个新的语言的第一个应用程序都会打印"Hellow,Word",在Swift中,可以只需要一行代码: pringln("Hello, word") ...

  6. asp.net:repeater嵌套(常用于新闻等在首页归类显示)

    using System;using System.Configuration;using System.Collections.Generic;using System.Linq;using Sys ...

  7. NET异步调用Webserver

    之前,有个同事跑来问我一堆的什么多线程异步进行调用Sap的服务再突然把进程关闭,还说要设置一个循环判断调用的结果,搞得我听的一头雾水,但是我明显感觉到他的设计思路已经渐行渐远了...已经再偏远的山区中 ...

  8. java面试入门总结

    最近正好有时间空下来,前一段时间本来打算呢,写一写阶段的总结,今天就来谈谈吧.作为一个java入门小白,之前就职于浙江大华,是通过大华10月份秋季招聘通过大华的面试. 浙江大华校招采用模式是先笔试.再 ...

  9. 数据结构学习——shell排序的C语言实现

    shell排序: 这个排序的命名是来自发明者的名字,和排序的方法没有字面上的联系.所以不要因为名字而感觉很难.在K&R的C程序设计语言中书中只用了几行代码很简洁的实现了这个排序算法.那就来看看 ...

  10. linux运维工程师,必须掌握以下几个工具

    本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了 ...