zoukankan      html  css  js  c++  java
  • HDU-4123-树形dp+rmq+尺取

    Bob’s Race

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3905    Accepted Submission(s): 1245


    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
             首先我们应该先用树形dp计算出从每个节点作为起点的最长路长度,将结果保存fm[i]中。对于每次询问需要知道区间最大/小值,这里用RMQ预处理后能在O(1)内计算出来。对于求最长的长度,一开始想的是二分,结果T了,后来换成尺取在O(N)内就能得到,少了个log就过了。
          ps:RMQ至今只会套板子= =
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 #define pii pair<int,int>
      4 #define mp make_pair
      5 #define pb push_back
      6 #define sf(a) scanf("%d",&a)
      7 #define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
      8 const int MAXN=50010;
      9 int N,M;
     10 int fm[MAXN],sm[MAXN];
     11 int fid[MAXN],sid[MAXN];
     12 int f1[MAXN][20],f2[MAXN][20];
     13 vector<pii>g[MAXN];
     14 void dfs1(int u,int fa)
     15 {
     16     fm[u]=sm[u]=0;
     17     fid[u]=sid[u]=0;
     18     for(int i=0;i<g[u].size();++i){
     19         int v=g[u][i].first;
     20         int w=g[u][i].second;
     21         if(v==fa) continue;
     22         dfs1(v,u);
     23         if(w+fm[v]>sm[u]){
     24             sm[u]=w+fm[v];
     25             sid[u]=v;
     26             if(sm[u]>fm[u]){
     27                 swap(fm[u],sm[u]);
     28                 swap(fid[u],sid[u]);
     29             }
     30         }
     31     }
     32 }
     33 void dfs2(int u,int fa,int w)
     34 {
     35     if(w+fm[fa]>sm[u]&&fid[fa]!=u){
     36         sm[u]=w+fm[fa];
     37         sid[u]=fa;
     38         if(sm[u]>fm[u]){
     39             swap(fm[u],sm[u]);
     40             swap(fid[u],sid[u]);
     41         }
     42     }
     43     if(w+sm[fa]>sm[u]&&sid[fa]!=u){
     44         sm[u]=w+sm[fa];
     45         sid[u]=fa;
     46         if(sm[u]>fm[u]){
     47             swap(fm[u],sm[u]);
     48             swap(fid[u],sid[u]);
     49         }
     50     }
     51     for(int i=0;i<g[u].size();++i){
     52         int v=g[u][i].first;
     53         int ww=g[u][i].second;
     54         if(v==fa) continue;
     55         dfs2(v,u,ww);
     56     }
     57 }
     58 void initRmq()
     59 {
     60     for(int i=0;i<N;++i){
     61         f1[i][0]=f2[i][0]=fm[i+1];
     62     }
     63     for(int j=1;(1<<j)<=N;j++){
     64         for(int i=0;i+(1<<j)-1<N;i++){
     65             f1[i][j]=min(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
     66             f2[i][j]=max(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
     67         }
     68     }
     69 }
     70 bool rmq(int L,int R,int Q)
     71 {
     72     L--;
     73     R--;
     74     int k=0;
     75     while((1<<(k+1))<=R-L+1) k++;
     76     int minn=min(f1[L][k],f1[R-(1<<k)+1][k]);
     77     int maxn=max(f2[L][k],f2[R-(1<<k)+1][k]);
     78     return maxn-minn<=Q;
     79 }
     80 int main()
     81 {
     82     int i,j,k;
     83     int u,v,w,Q;
     84     while(cin>>N>>M&&(N||M)){
     85         for(i=1;i<N;++i){
     86             sfff(u,v,w);
     87             g[u].push_back(mp(v,w));
     88             g[v].push_back(mp(u,w));
     89         }
     90         dfs1(1,0);
     91         dfs2(1,0,0);
     92         initRmq();
     93         while(M--){
     94             sf(Q);
     95             int l=1,r=1,ans=0;          //二分会T,这里用尺取能达到O(N)
     96             int n=N;
     97              for(l=1;l<=n;++l){
     98                  while(r<=n&&rmq(l,r,Q)) r++;
     99                  ans=max(ans,r-l);
    100              }
    101             printf("%d
    ",ans);
    102         }
    103         for(i=1;i<=N;++i)g[i].clear();
    104     }
    105     return 0;
    106 }
    Source
  • 相关阅读:
    codeblocks-17.12mingw-nosetup(mingw编译,绿色免安装版)的下载、安装及设置一
    我的Qt历程1:第一个Qt程序
    单一职责-依赖倒转-代理模式-迭代器模式等
    结合你以往的工作经验谈谈高并发应用的性能优化措施
    如何优化数据库,如何提高数据库的性能?
    在一个千万级的数据库查询中,如何提高查询效率?
    如何提高页面的显示速度?假如一个页面的加载时间是10.89s,你会用什么方式进行优化?
    强制提高网站性能有什么办法
    简述httpModule与HttpHandler
    EntityFramework的效率与ADO.Net的效率哪个高?
  • 原文地址:https://www.cnblogs.com/zzqc/p/8848420.html
Copyright © 2011-2022 走看看