编程介的小学生 2017-11-19 09:49 采纳率: 20.5%
浏览 582
已结题

Bob’s Race

Problem 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

  • 写回答

1条回答 默认 最新

  • $(X) 2018-07-17 15:11
    关注
    /*from:   https://www.cnblogs.com/kuangbin/p/3414812.html*/ 
    /* ***********************************************
    Author        :kuangbin
    Created Time  :2013-11-8 16:56:11
    File Name     :E:\2013ACM\专题强化训练\区域赛\2011福州\C.cpp
    ************************************************ */
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    const int MAXN = 50010;
    struct Edge
    {
        int to,next;
        int w;
    }edge[MAXN*2];
    int head[MAXN],tot;
    void init()
    {
        tot = 0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,int w)
    {
        edge[tot].to = v;
        edge[tot].w = w;
        edge[tot].next = head[u];
        head[u] = tot++;
    }
    int maxn[MAXN],smaxn[MAXN];
    int maxid[MAXN],smaxid[MAXN];
    void dfs1(int u,int pre)
    {
        maxn[u] = smaxn[u] = maxid[u] = smaxid[u] = 0;
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            int v = edge[i].to;
            if(pre == v)continue;
            dfs1(v,u);
            if(maxn[v] + edge[i].w > smaxn[u])
            {
                smaxid[u] = v;
                smaxn[u] = maxn[v] + edge[i].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].to;
            if(pre == v)continue;
            if(maxid[u] == v)
            {
                if(smaxn[u] + edge[i].w > smaxn[v])
                {
                    smaxn[v] = smaxn[u] + edge[i].w;
                    smaxid[v] = u;
                    if(maxn[v] < smaxn[v])
                    {
                        swap(maxn[v],smaxn[v]);
                        swap(maxid[v],smaxid[v]);
                    }
                }
            }
            else
            {
                if(maxn[u] + edge[i].w > smaxn[v])
                {
                    smaxn[v] = maxn[u] + edge[i].w;
                    smaxid[v] = u;
                    if(maxn[v] < smaxn[v])
                    {
                        swap(maxn[v],smaxn[v]);
                        swap(maxid[v],smaxid[v]);
                    }
                }
            }
            dfs2(v,u);
        }
    }
    int a[MAXN];
    
    int dp1[MAXN][20];
    int dp2[MAXN][20];
    int mm[MAXN];
    void initRMQ(int n)
    {
        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] = a[i];
            dp2[i][0] = a[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("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n,m;
        int u,v,w;
        int Q;
        while(scanf("%d%d",&n,&m) == 2)
        {
            if(n == 0 && m == 0)break;
            init();
            for(int i = 1;i < n;i++)
            {
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            dfs1(1,1);
            dfs2(1,1);
            for(int i = 1;i <= n;i++)
                a[i] = maxn[i];
            initRMQ(n);
            while(m--)
            {
                scanf("%d",&Q);
                int ans = 0;
                int 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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。