Bob’s Race HDU - 4123 (树形DP+RMQ+区间队列)

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

题意:给定一棵树,你需要求出任意一点能到达的最远距离,然后给m个询问,每次给一个值q,问 1到n 这个区间中,最大值与最小值的差不超过q的最大区间长度。

思路:先求出每个点能到达的最远距离,树形dp,dfs两次就好了,dp[i][0]表示向下能到达的最远距离,dp[i][1]表示向下能到达的次远距离,dp[i][2]表示向上能到达的最远距离,最后求出f[i]表示能到达的最远距离。

接着是处理询问,使用rmq预处理出每个区间的最值

最后一步,设两个指针,l,r初始化为1,然后依次对r,l进行右移,并记录最大区间长度。

代码:

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
#include<sstream>
#include<cstdio>
#include<unordered_map>
#include<unordered_set>
#include<cmath>
#include<climits>
using namespace std;
const int maxn=1e5+9;
int head[maxn];
int num;
int dp[maxn][3];
int location[maxn];
int f[maxn];
int Max[maxn][20],Min[maxn][20];
int idx[maxn];
int n,m;
struct Edge
{
    int u,v,w,next;
}edge[maxn<<1];
void addEdge(int u,int v,int w)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(dp,0,sizeof(dp));
    memset(f,0,sizeof(f));
    memset(Max,0,sizeof(Max));
    memset(Min,0,sizeof(Min));
    num=0;
}
void dfs(int u,int pre)
{
    int mx0=0, mx1=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v,w=edge[i].w;
        if(v==pre) continue;
        dfs(v,u);
        if(mx0<dp[v][0]+w)
        {
            mx1=mx0;
            idx[u]=v;
            mx0=dp[v][0]+w;
        }
        else if(mx1<dp[v][0]+w)
        {
            mx1=dp[v][0]+w;
        }
        else if(mx1<dp[v][1]+w)
        {
            mx1=dp[v][1]+w;
        }
    }
    dp[u][0]=mx0,dp[u][1]=mx1;
}
void dfs2(int u,int pre)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v,w=edge[i].w;
        if(pre==v) continue;
        if(idx[u]==v)
        {
            dp[v][2]=max(dp[u][2],dp[u][1])+w;
        }
        else
        {
            dp[v][2]=max(dp[u][2],dp[u][0])+w;
        }
        dfs2(v,u);
    }
}
void rmq()
{
    location[0]=-1;
    for(int i=1;i<=n;i++)
    {
        location[i]=(i&(i-1))==0?location[i-1]+1:location[i-1];
        Max[i][0]=f[i];
        Min[i][0]=f[i];
    }
    for(int j=1;(1<<j)<=n;j++)
    {

        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
            Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
        }
    }
}
int query(int l,int r)
{
    int k=location[r-l+1];
    return max(Max[l][k],Max[r-(1<<k)+1][k])- \
           min(Min[l][k],Min[r-(1<<k)+1][k]);
}
void solve(int q)
{
    int l=1,r=1;
    int ans=1;
    while(l<=n&&r<=n)
    {
        while(r<=n&&query(l,r)<=q)
        {
            r++;
        }
        ans = max(ans,r-l);
        l++;
    }
    printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d%d",&n,&m)!=EOF&&n+m)
    {
        init();
        for(int i=1;i<n;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            addEdge(x,y,w);
            addEdge(y,x,w);
        }
        dfs(1,-1);
        dfs2(1,-1);
        for(int i=1;i<=n;i++)
        {
            f[i]=max(dp[i][0],dp[i][2]);
        }
        rmq();
        while(m--)
        {
            int q;
            scanf("%d",&q);
            solve(q);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81436041
今日推荐