码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3162.Walking Race 树形dp

时间:2017-10-02 16:17:11      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:ring   技术分享   algorithm   which   training   sed   pre   art   instr   

Walking Race
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

 
题意:有一棵n个节点的树,每天从i点开始跑,跑最远能到达的距离,求一段连续的天数,这些天里面跑的最多的和最少的距离相差不能超过m,求最多的连续天数。
思路:树形dp。
代码:
技术分享
#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+100,M=2e6+100;
const int inf=0x3f3f3f3f;
const ll INF=1e13+7,mod=1e9+7;
struct edge
{
    int from,to;
    ll w;
    int next;
};
edge es[M];
int cnt,head[N];
ll dp[N][5];
void init()
{
    cnt=0;
    memset(head,-1,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][0]=dp[u][1]=0;
    for(int i=head[u]; i!=-1; i=es[i].next)
    {
        edge e=es[i];
        if(e.to==fa) continue;
        dfs1(e.to,u);
        ll tmp=dp[e.to][0]+e.w;
        if(tmp>dp[u][0]) swap(tmp,dp[u][0]);
        if(tmp>dp[u][1]) swap(tmp,dp[u][1]);
    }
}
void dfs2(int u,int fa)
{
    for(int i=head[u]; i!=-1; i=es[i].next)
    {
        edge e=es[i];
        if(e.to==fa) continue;
        if(dp[e.to][0]+e.w==dp[u][0])
            dp[e.to][2]=max(dp[u][2],dp[u][1])+e.w;
        else dp[e.to][2]=max(dp[u][2],dp[u][0])+e.w;
        dfs2(e.to,u);
    }
}
ll Tree1[N<<2],Tree2[N<<2];
void pushup(int pos)
{
    Tree1[pos]=max(Tree1[pos<<1],Tree1[pos<<1|1]);
    Tree2[pos]=min(Tree2[pos<<1],Tree2[pos<<1|1]);
}
void build(int l,int r,int pos)
{
    if(l==r)
    {
        Tree1[pos]=Tree2[pos]=max(dp[l][0],dp[l][2]);
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,pos<<1);
    build(mid+1,r,pos<<1|1);
    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)>>1;
    if(L<=mid) query(ans1,ans2,L,R,l,mid,pos<<1);
    if(R>mid) query(ans1,ans2,L,R,mid+1,r,pos<<1|1);
}
int main()
{
    int n;
    ll m;
    scanf("%d%lld",&n,&m);
    init();
    for(int i=2,j; i<=n; i++)
    {
        ll w;
        scanf("%d%lld",&j,&w);
        addedge(i,j,w),addedge(j,i,w);
    }
    memset(dp,0,sizeof(dp));
    dfs1(1,0);
    dfs2(1,0);
    int ans=0;
    build(1,n,1);
    for(int i=1,j=1; i<=n&&j<=n;)
    {
        ll maxx=-INF,minx=INF;
        query(maxx,minx,i,j,1,n,1);
        if(maxx-minx<=m) ans=max(ans,j-i+1),j++;
        else i++;
        if(n-i<ans) break;
    }
    printf("%d\n",ans);
    return 0;
}
树形dp

 

POJ 3162.Walking Race 树形dp

标签:ring   技术分享   algorithm   which   training   sed   pre   art   instr   

原文地址:http://www.cnblogs.com/GeekZRF/p/7620211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!