hdu4123Bob’s Race【树型dp求树上区间范围次小值】

Description

Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses, and all houses are connected together. To make the race more interesting, he requires that every participant must start from a different house and run AS FAR AS POSSIBLE without passing a road more than once. The distance difference between the one who runs the longest distance and the one who runs the shortest distance is called “race difference” by Bob. Bob does not want the “race difference”to be more than Q. The houses are numbered from 1 to N. Bob wants that the No. of all starting house must be consecutive. He is now asking you for help. He wants to know the maximum number of starting houses he can choose, by other words, the maximum number of people who can take part in his race.
 

Input

There are several test cases.
The first line of each test case contains two integers N and M. N is the number of houses, M is the number of queries.
The following N-1 lines, each contains three integers, x, y and z, indicating that there is a road of length z connecting house x and house y.
The following M lines are the queries. Each line contains an integer Q, asking that at most how many people can take part in Bob’s race according to the above mentioned rules and under the condition that the“race difference”is no more than Q.

The input ends with N = 0 and M = 0.

(N<=50000 M<=500 1<=x,y<=N 0<=z<=5000 Q<=10000000)
 

Output

For each test case, you should output the answer in a line for each query.
 

Sample Input

    
    
5 5 1 2 3 2 3 4 4 5 3 3 4 2 1 2 3 4 5 0 0
 

Sample Output

    
    
1 3 3 3 5
 

其实也就是把它分类到树型dp里面作为练习,我才这么分类的,个人觉得就是一个普通的dfs版dp啊

题意:求树上最长连续区间使得最大值-最小值的差小于q。纠结了半个礼拜为啥要求次小值,今天问了学弟才发现自己又犯二了,orz

首先,我们要理解好两次dfs的目的,第一次dfs求出的是某点到达它下面的叶子节点的最长距离,第二次dfs要求的是对于某点而言总体的最大值。由于最大值有可能是向上查找到父节点再折下去的(注意是父节点折下去的,要是考虑成根节点这题就没法做了)。如果v是u最长路径上的点,那么需要比较的值是u次小值+u,v距离~


/*************
hdu4123
2016.4.2
11904 KB	811 ms
C++
**************/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=50010;
int tol,head[MAXN],n;
struct Edge
{
    int w,v,next;
}edge[MAXN<<1];
int maxn[MAXN],maxid[MAXN],smaxn[MAXN],smaxid[MAXN];
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[tol].v=v;edge[tol].w=w;edge[tol].next=head[u];head[u]=tol++;
}
void dfs1(int u,int pre)
{
    maxn[u]=maxid[u]=smaxn[u]=smaxid[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(v==pre) continue;
        dfs1(v,u);
        if(maxn[v]+w>smaxn[u])
        {
            smaxid[u]=v;
            smaxn[u]=maxn[v]+w;
            if(maxn[u]<smaxn[u])
            {
                swap(maxn[u],smaxn[u]);
                swap(maxid[u],smaxid[u]);
            }
        }
    }
}
void dfs2(int u,int pre)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(v==pre) continue;
        if(maxid[u]==v)
        {
            if(smaxn[u]+w>smaxn[v])
            {
                smaxid[v]=u;
                smaxn[v]=smaxn[u]+w;
                if(maxn[v]<smaxn[v])
                {
                    swap(maxn[v],smaxn[v]);
                    swap(maxid[v],smaxid[v]);
                }
            }
        }
        else
        {
            if(maxn[u]+w>smaxn[v])
            {
                smaxid[v]=u;
                smaxn[v]=maxn[u]+w;
                if(maxn[v]<smaxn[v])
                {
                    swap(maxn[v],smaxn[v]);
                    swap(maxid[v],smaxid[v]);
                }
            }
        }
        dfs2(v,u);
    }
}
int dp1[MAXN][20],dp2[MAXN][20],mm[MAXN];
void initrmq()
{
    mm[0]=-1;
    for(int i=1;i<=n;i++)
    {
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        dp1[i][0]=maxn[i];
        dp2[i][0]=maxn[i];
    }
    for(int j=1;j<=mm[n];j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
            dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
        }
}
int rmq(int x,int y)
{
    int k=mm[y-x+1];
    return max(dp1[x][k],dp1[y-(1<<k)+1][k])-min(dp2[x][k],dp2[y-(1<<k)+1][k]);
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int m;
    while(~scanf("%d%d",&n,&m)&&n&&m)
    {
        init();
        for(int i=1;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        dfs1(1,-1);
        dfs2(1,-1);
        initrmq();
        while(m--)
        {
            int q;
            scanf("%d",&q);
            int ans=0,id=1;
            for(int i=1;i<=n;i++)
            {
                while(id<=i&&rmq(id,i)>q)id++;
                ans=max(ans,i-id+1);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值