hdu 4123 树形dp+rmq

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

 

一道比较正常的题目,算是简单题。题意不说了。

 

定义down[u]:从当前结点开始往子节点方向走最长距离,up[u]:从当前结点开始往父节点方向走最长距离。

然后存下每个点开始走的话能走的最长距离。

下面用rmq维护,求得每次询问的结果,思路很好想。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int MAXM = 50005*2;//边的个数要注意是n的两倍
const int MAXN = 50005;

///树形dp部分

struct Edge
{
    int dis;
    int to,next;
} edge[MAXM];
int head[MAXN],tot;
void addedge(int u,int v,int dis)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    edge[tot].dis = dis;
    head[u] = tot++;
}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

int down[MAXN],up[MAXN];
void cal_down(int u , int pre)
{
    down[u] = up[u] = 0;
    for(int e=head[u]; e!=-1; e=edge[e].next)
    {
        int v = edge[e].to , dis = edge[e].dis;
        if(v==pre) continue;
        cal_down(v,u);
        down[u] = max(down[u],down[v]+dis);
    }
}

void cal_up(int u , int pre)
{
    stack<int> sta;
    int max_down=0;
    for(int e=head[u]; e!=-1; e=edge[e].next)
    {
        int v = edge[e].to , dis = edge[e].dis;
        if(v==pre) continue;
        sta.push(e);
        up[v] = dis + up[u];
        up[v] = max(up[v] , max_down+dis);
        max_down = max(max_down,down[v]+dis);
    }
    max_down = 0;
    while(!sta.empty())
    {
        int e = sta.top();
        sta.pop();
        int v = edge[e].to , dis = edge[e].dis;
        up[v] = max(up[v] , max_down+dis);
        max_down = max(max_down,down[v]+dis);
        cal_up(v,u);
    }
}

///rmq部分

int dp_min[MAXN][20],dp_max[MAXN][20];
int mm[MAXN];
//初始化RMQ, b数组下标从1开始,从0开始简单修改
void initRMQ(int n,int b[])
{
    mm[0] = -1;
    for(int i = 1; i <= n; i++)
    {
        mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
        dp_max[i][0] = b[i];
        dp_min[i][0] = b[i];
    }
    for(int j = 1; j <= mm[n]; j++)
        for(int i = 1; i + (1<<j) -1 <= n; i++)
        {
            dp_max[i][j] = max(dp_max[i][j-1],dp_max[i+(1<<(j-1))][j-1]);
            dp_min[i][j] = min(dp_min[i][j-1],dp_min[i+(1<<(j-1))][j-1]);
        }
}
//查询最大值
int q_max(int x,int y)
{
    int k = mm[y-x+1];
    return max(dp_max[x][k],dp_max[y-(1<<k)+1][k]);
}
int q_min(int x,int y)
{
    int k = mm[y-x+1];
    return min(dp_min[x][k],dp_min[y-(1<<k)+1][k]);
}

int main()
{
    int n,m,dis,x,y;
    while(scanf("%d%d",&n,&m)==2&&n&&m)
    {
        init();
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&x,&y,&dis);
            addedge(x,y,dis);
            addedge(y,x,dis);
        }
        cal_down(1,-1);
        cal_up(1,-1);

        for(int i=1; i<=n; i++)
        {
            up[i] = max(up[i],down[i]);
        }

        int Q;

        initRMQ(n,up);

        while(m--)
        {
            int ans=0;
            scanf("%d",&Q);
            int i=1,j=1;
            while(j<=n)
            {
                int X = q_max(i,j)-q_min(i,j);
                if(X > Q) i++;
                else
                {
                    ans=max(ans,j-i+1);
                    j++;
                }
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
posted @ 2016-04-06 17:16  ZhMZ  阅读(124)  评论(0编辑  收藏  举报