zoukankan      html  css  js  c++  java
  • hdu 4123 Bob’s Race (dfs树上最远距离+RMQ)

    C - Bob’s Race
    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
     

    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
     
     
    题意: 给一颗无向有权树,选择一些连续的点作为起点,每个起点u有一个人跑步要尽可能的跑得远,用ai表示第i个点作为起点能跑得最远的距离,起点选择l...r这些点,那么对每个询问求一对l和r,满足max(al...ar) - min(al...ar) <= Q, 要求l和r的差值尽量大,输出这个最大差值
    思路: 首先dfs+dp求每个点u能跑的最远距离,然后对每个询问用一个双指针扫一遍过去,复杂度o(n)
     
    代码
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 50010;
    
    struct _edge{
        int to,w,next;
    };
    _edge edge[N<<1];
    int ecnt,head[N];
    inline void addedge(int u,int v,int w)
    {
        edge[ecnt].to = v;
        edge[ecnt].w = w;
        edge[ecnt].next = head[u];
        head[u]=ecnt++;
    }
    
    int n,m,a[N];
    int dp[N][2],id[N][2];
    
    void dfs1(int u,int fa)
    {
        dp[u][0]=dp[u][1]=0;
        for(int e=head[u];e!=-1;e=edge[e].next)
        {
            int &v = edge[e].to;
            if(v==fa) continue;
            dfs1(v,u);
            int t1 = dp[v][0] + edge[e].w, t2 = v;
            if(t1>=dp[u][0])
            {
                swap(t1,dp[u][0]);
                swap(t2,id[u][0]);
            }
            if(t1>=dp[u][1])
            {
                swap(t1,dp[u][1]);
                swap(t2,id[u][1]);
            }
        }
    }
    
    void dfs2(int u,int fa,int up)
    {
        a[u]=max(dp[u][0],up);
        for(int e=head[u];e!=-1;e=edge[e].next)
        {
            int &v = edge[e].to;
            if(v==fa) continue;
            int t;
            if(v==id[u][0])
                t = max(dp[u][1],up);
            else
                t = max(dp[u][0],up);
            dfs2(v,u,t+edge[e].w);
        }
    }
    
    int dmx[N][20],dmn[N][20];
    void RMQ_init()
    {
        for(int i=1;i<=n;i++) dmn[i][0]=dmx[i][0]=a[i];
        for(int j=1;(1<<j)<=n;j++)
            for(int i=0;i+(1<<j)-1<=n;i++)
                dmn[i][j]=min(dmn[i][j-1],dmn[i+(1<<(j-1))][j-1]),
                dmx[i][j]=max(dmx[i][j-1],dmx[i+(1<<(j-1))][j-1]);
    }
    void query(int l,int r,int &mn,int &mx)
    {
        int k = 0;
        while((1<<(k+1))<=r-l+1) k++;
        mn = min(dmn[l][k],dmn[r-(1<<k)+1][k]);
        mx = max(dmx[l][k],dmx[r-(1<<k)+1][k]);
    }
    
    void run()
    {
        int u,v,w;
        ecnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        memset(dp,-1,sizeof(dp));
        memset(id,-1,sizeof(id));
        dfs1(1,-1);
        dfs2(1,-1,0);
    //    for(int i=1;i<=n;i++)
    //        cout<<a[i]<<' ';
    //    cout<<endl;
    
        RMQ_init();
    
        int q;
        while(m--)
        {
            scanf("%d",&q);
            int ans = 0;
            int l=1,r;
            int mn,mx;
            mn=mx=a[1];
            for(r=2;r<=n;r++)
            {
                if(a[r]>mx) mx=a[r];
                if(a[r]<mn) mn=a[r];
                while(mx-mn>q)
                {
                    l++;
                    query(l,r,mn,mx);
                }
                if(ans < r-l+1)
                    ans = r-l+1;
            }
            printf("%d
    ",ans);
        }
    }
    
    int main()
    {
       // freopen("in","r",stdin);
        while(scanf("%d%d",&n,&m)!=EOF && n)
            run();
        return 0;
    }
     
     
     
  • 相关阅读:
    有了这些开源动效项目,设计和开发不再相杀只剩相爱
    优雅地使用 C++ 制作表格:tabulate
    编写 Django 应用单元测试
    Django Haystack 全文检索与关键词高亮
    JAVA格物致知开篇:凡事预则立不预则废
    前端见微知著流程篇:前端开发流程总结
    前端见微知著工具篇:Bower组件管控
    前端见微知著工具篇:Grunt实现自动化
    前端见微知著AngularJS备忘篇:温故而知新,可以为师矣
    前端见微知著JavaScript基础篇:this or that ?
  • 原文地址:https://www.cnblogs.com/someblue/p/4017834.html
Copyright © 2011-2022 走看看