POJ 3162.Walking Race 树形dp 树的直径
| 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
#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 树的直径的更多相关文章
- POJ 3162 Walking Race 树形DP+线段树
给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...
- POJ - 3162 Walking Race 树形dp 单调队列
POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...
- 【题解】poj 3162 Walking Race 树形dp
题目描述 Walking RaceTime Limit: 10000MS Memory Limit: 131072KTotal Submissions: 4941 Accepted: 1252Case ...
- POJ 3162 Walking Race 树形dp 优先队列
http://poj.org/problem?id=3162 题意 : 一棵n个节点的树.wc爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 4607 树形dp 树的直径
题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- VIJOS1476旅游规划[树形DP 树的直径]
描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...
随机推荐
- 关键字new与malloc函数
做题出bug,OJ说我没有定义new. 纳尼?new还需要定义?不都是直接用的吗,明明在自己的编译器里都可以通过的! 编译器:劳资是C++.... 一番倒腾后发现,我用的C++,但是OJ的编译器是C, ...
- QTP - 描述性编程
描述性编程: 1.QTP的描述性编程能够摆脱测试对象库的限制,编写出更为复杂.适应能力更强的测试脚本. 2.即不需要在仓库晨定义,也能访问和操作实际对象. 3.用描述性编程编写的测试脚本在运行时,QT ...
- javascript判断字符串相等
- 在linux上安装docker
我的linux系统是阿里云服务器,是centos版本的. 前置条件 64-bit 系统 kernel 3.10+ 用uname -r命令检查内核版本,返回的值大于3.10即可. 用sudo wget ...
- java 深度复制与浅复制 copyOf、arraycopy、copyOfRange
1.copyOf 原型:public static <T> T[] copyOf(T[] original, int newLength) original:原数组 newLength:要 ...
- hbase 存储结构和原理
HBase的表结构 建表时要指定的是:表名.列族 建表语句 create 'user_info', 'base_info', 'ext_info' 意思是新建一个表,名称是user_info,包含两个 ...
- JavaScript遍历集合(for...of/for...in/forEach)
var arr = [1,2,3]; var map = new Map(); map.set('baylor',22); var s = new Set(); s.add([1,2,3]); for ...
- NetworkStream介绍说明
如果服务器和客户端之间基于TCP连接的,他们之间能够依靠一个稳定的字节流进行相互传输信息,这也是NetworkStream的最关键的作用,有了这个神奇的协议,NetWorkStream便能向其他流一样 ...
- java 将mysql中Blob类型转为字符串或数字
引入Blob类型貌似不行,不知道是版本问题还是java中的Blob类型为oracle,后来使用byte[]转换成功. public float byte2float(byte[] b) { if(b! ...
- 利用ajax与input 上传与下载文件
html 部分代码<form action="" method="" class="form form-horizontal" nov ...