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;
    }
     
     
     
  • 相关阅读:
    《虚拟伙伴》AR增强现实应用开发总结
    捕获起英文名Edda的灵感来源,我的心愿是程序员这个行业能够男女人数平衡
    侯捷老师C++大系之C++面向对象开发:(一)不带指针的类:Complex复数类的实现过程
    【腾讯GAD暑期训练营游戏程序班】游戏中的物理系统作业说明文档
    socketAPI:一个最简单的服务器和对应的客户端C语言的实现
    C/C++实践笔记 008
    C/C++实践笔记 007
    学习java之泛型类和泛型方法
    学习java之利用泛型访问自己定义的类
    学习java之HashMap和TreeMap
  • 原文地址:https://www.cnblogs.com/someblue/p/4017834.html
Copyright © 2011-2022 走看看