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市的交通网络十分简单,它包 ...
随机推荐
- Jmeter使用插件监控服务器资源的使用情况
环境准备 客户端: 1)安装最新版的Jmeter,目前为 Jmeter5.0 2)安装插件管理器:JMeterPlugin jmeter-plugins-manager.jar下载完成之后,将下载的j ...
- Exp6 信息搜集与漏洞扫描 20164313 杜桂鑫
1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 1.使用搜索引擎 在百度搜索栏内输入 site:com filetype:doc 北京电子科技学院 ...
- mysql python 交互
一.安装 sudo apt-get install pymysql sudo pip3 install pymysql Connection对象 用于建立与数据库的连接 创建对象:调用connect( ...
- jQuery validdate插件的使用
跟着书上的例子做的时候发现很奇怪,在script标签中用message重写提示消息,没有反应,不知道是不是引入的metadata.js文件有问题,于是用了菜鸟教程的cdn和教程,魔改了一下 <! ...
- 嵌入式linux——说明(零)
之前就学习过嵌入式linux,但是那时候并没有完全投入,学习的也不科学系统,没有笔记,也没有自己写很多的代码来练习,所以到现在是基本归零了,现在比较有富裕的时间来系统的学习,从今天开始要克服每一个学习 ...
- cookie和session的区别及在Django中应用
Django中Cookie和session应用 什么是cookie? cookie是客户端浏览器上的一个文件,以键值对进行保存,类似于字典的 {'key' : 'value'} ,与服务器端没有关系, ...
- open()函数 linux中open函数使用
来源:http://www.cnblogs.com/songfeixiang/p/3733855.html linux中open函数使用 open函数用来打开一个设备,他返回的是一个整型变量,如果 ...
- numpy-matrix 方法速查
->matrix.T transpose:返回矩阵的转置矩阵 matrix.H hermitian (conjugate) transpose:返回复数矩阵的共轭元素矩阵 matrix.I in ...
- 基于Ubuntu系统XAMPP环境安装以及DVWA渗透测试系统安装(详解的不能再详解了)
首先这是X勺年人生第二次博,用来记录一下转折于我而言.做个简介,这个Ubuntu是linux的一种吧,然 然后这个Ubuntu是我刚简单安装(在虚拟机上)没有经过任何配置,从头开始,我这绝壁的小白,其 ...
- [原创] 扩展jquery-treegrid插件, 实现勾选功能和全删按钮.
新上手一个项目, 因而正好想学习下bootstrap, 所以就采用asp.net mvc + bootstrap来做. 因为需要TreeGrid的控件, 本来想用easyUI.LingerUi.DW ...