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市的交通网络十分简单,它包 ...
随机推荐
- Android 开发 RecyclerView设置间距
实现步骤 首先要创建一个类继承RecyclerView.ItemDecoration 然后重新这个类的getItemOffsets方法,删除方法里的super.getItemOffsets(outRe ...
- 定制你自己的vim编辑器
今天定制了一下自己的vim编辑器,效果嘛,谁用谁知道!话不多说,直奔主题.vim编辑器的配置都在/etc/vimrc文件中. #vim /etc/vimrc 打开配置文件,在尾部添加如下的,不是全都必 ...
- spring boot application.properties详解
附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...
- python学习笔记----正则表达式
正则: regular expression 常用的场景: #正则的包 >>> import re #match:开头匹配,匹配到,返回一个匹配对象,否则返回None >> ...
- 分享一个微信自动跳转外部浏览器下载app的api接口!
现在微信渠道可以说是拉新最快的渠道,因为微信具备强裂变性.但是目前微信对第三方下载链接的拦截是越来越严格了,那么想要在微信内肆无忌惮地推广链接就需要用到微信跳转浏览器的接口,那如何获取该接口呢? ...
- css:伪类和伪元素
一:伪类 1. :active 想被激活的元素添加样式 2. :focus 向拥有键盘输入焦点的元素添加样式 3. :hover 当鼠标悬浮在元素上方时,向元素添加样式 4. ...
- 【译】深度双向Transformer预训练【BERT第一作者分享】
目录 NLP中的预训练 语境表示 语境表示相关研究 存在的问题 BERT的解决方案 任务一:Masked LM 任务二:预测下一句 BERT 输入表示 模型结构--Transformer编码器 Tra ...
- mysql安装好之后,查询显示MySQL不是内部命令或外部命令问题
使用cmd来调用MySQL的时候提示错误,错误是说MySQL不是内部或外部命令. 1.如图所示,遇到的mysql命令错误. 2.现在就要查询mysql是安装在哪,我们在计算机里面搜索mysql.exe ...
- docker swarm 集群及可视化界面的安装及配置
docker swarm 集群及可视化界面的安装及配置 2016-12-14 16:08:46 标签:swarm consul registrator 原创作品,允许转载,转载时请务必以超链接形式标明 ...
- servlet实现mysql数据库分页
一.分页所需要的sql语句准备 select * from table limit m,n其中m是指记录开始的index,从0开始,表示第一条记录n是指从第m+1条开始,取n条. 例如:select ...