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市的交通网络十分简单,它包 ...
随机推荐
- Webservice 从客户端中检测到有潜在危险的 request.form值[解决方法]
<system.web> <httpRuntime requestValidationMode="2.0" /> <pages validateReq ...
- 用ASP.NET_Regsql.exe创建Session数据库
CMD: C:\Users\ZhangSC>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regsql.exe -S ZhangS ...
- 使用jTessBoxEditorFX训练Tesseract-OCR教程
使用jTessBoxEditorFX训练Tesseract-OCR教程 注:1,工具是JAVA编写的,所以在使用工具之间,需要安装JAVA环境. 2,安装Tesseract-OCR应用程序,并将目录添 ...
- 4-17疑难点 c语言之【结构体对齐】
今天学习了结构体这一章节,了解到了结构体在分配内存的时候采取的是对齐的方式 例如: #include<stdio.h> struct test1 { int a; char b; shor ...
- iOS Simulator version 11 or later is currently not supported.
iOS Simulator version 11 or later is currently not supported.You can open Xcode > Preferences > ...
- mybatis中两种取值方式?谈谈Spring框架理解?
1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...
- Delphi 窗口操作
unit UnitWinUtils; interface uses Windows; Type TDWA128=Array [..] of LongWord; TDWA256=Array [..] o ...
- 自学PHP的正确方法与经验
我是2015年开始接触认识到PHP编程方面的知识,2012年我还是一名刚毕业的大学生开始踏入社会从事自己一份学校推荐的自动化职业,自动化工作枯燥无味,每天基本上3点一线,食堂-公司机器-宿舍,做了3年 ...
- json字符串和json对象之间的转化
一.json字符串转化为json对象 二.json对象转化为json字符串
- inpuy只能输入数字,并且禁止输入e
<el-input type="number" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" ...