zoukankan      html  css  js  c++  java
  • hdu 4123(树形dp+倍增)

    Bob’s Race

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

    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
    Source
     
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<cstdlib>
      6 #include<string.h>
      7 #include<set>
      8 #include<vector>
      9 #include<queue>
     10 #include<stack>
     11 #include<map>
     12 #include<cmath>
     13 typedef long long ll;
     14 typedef unsigned long long LL;
     15 using namespace std;
     16 const double PI=acos(-1.0);
     17 const double eps=0.0000000001;
     18 const int N=50000+100;
     19 int head[N];
     20 int dp1[N][30],dp2[N][30];
     21 int tot;
     22 int n;
     23 struct node{
     24     int to,next,w;
     25 }edge[N<<1];
     26 int dp[N][3];
     27 int vis[N];
     28 int a[N];
     29 void init(){
     30     memset(head,-1,sizeof(head));
     31     memset(dp,0,sizeof(dp));
     32     tot=0;
     33 }
     34 void add(int u,int v,int w){
     35     edge[tot].to=v;
     36     edge[tot].next=head[u];
     37     edge[tot].w=w;
     38     head[u]=tot++;
     39 }
     40 void DFS(int u,int fa){
     41     int maxx1=0;
     42     int maxx2=0;
     43     for(int i=head[u];i!=-1;i=edge[i].next){
     44         int v=edge[i].to;
     45         int w=edge[i].w;
     46         if(v==fa)continue;
     47         DFS(v,u);
     48         int maxx=dp[v][0]+w;
     49         if(maxx>=maxx1){
     50             maxx2=maxx1;
     51             maxx1=maxx;
     52             vis[u]=v;
     53         }
     54         else if(maxx>maxx2){
     55             maxx2=maxx;
     56         }
     57        // cout<<u<<" "<<v<<" "<<maxx1<<" "<<maxx2<<endl;
     58     }
     59     dp[u][0]=maxx1;
     60     dp[u][1]=maxx2;
     61 }
     62 void DFS1(int u,int fa){
     63     for(int i=head[u];i!=-1;i=edge[i].next){
     64         int v=edge[i].to;
     65         int w=edge[i].w;
     66         if(v==fa)continue;
     67         if(vis[u]==v){
     68             dp[v][2]=max(dp[u][1]+w,dp[u][2]+w);
     69         }
     70         else{
     71             dp[v][2]=max(dp[u][0]+w,dp[u][2]+w);
     72         }
     73         DFS1(v,u);
     74     }
     75 }
     76 void init_RMQ(){
     77     for(int i=1;i<=n;i++){
     78         dp1[i][0]=a[i];
     79         dp2[i][0]=a[i];
     80     }
     81     for(int j=1;(1<<j)<=n;j++)
     82     for(int i=1;i+(1<<j)-1<=n;i++){
     83         dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
     84         dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
     85     }
     86 }
     87 int getRMQ(int L,int R){
     88     int k=0;
     89     while((1<<(k+1))<=R-L+1)k++;
     90     int maxx=max(dp1[L][k],dp1[R-(1<<k)+1][k]);
     91     int minn=min(dp2[L][k],dp2[R-(1<<k)+1][k]);
     92     return maxx-minn;
     93 }
     94 
     95 int main(){
     96     int m;
     97     while(scanf("%d%d",&n,&m)!=EOF){
     98 
     99         if(m==0&&n==0)break;
    100         init();
    101         for(int i=2;i<=n;i++){
    102             int u,v,w;
    103             scanf("%d%d%d",&u,&v,&w);
    104             add(u,v,w);
    105             add(v,u,w);
    106         }
    107         DFS(1,-1);
    108         DFS1(1,-1);
    109         for(int i=1;i<=n;i++){
    110             a[i]=max(dp[i][0],dp[i][2]);
    111             //cout<<a[i]<<" ";
    112         }
    113         init_RMQ();
    114         for(int i=0;i<m;i++){
    115             int x;
    116             scanf("%d",&x);
    117             int l=1;
    118             int r=1;
    119             int ans=1;
    120             while(l<=n&&r<=n){
    121                 while(r<=n&&getRMQ(l,r)<=x){
    122                     r++;
    123                 }
    124                 ans=max(ans,r-l);
    125                 l++;
    126             }
    127             cout<<ans<<endl;
    128         }
    129     }
    130 }
  • 相关阅读:
    ExplorerControls的显示问题
    VS选项中没有C#相关设置选项?
    根据点提取栅格值
    原来我的代码暴露在外面!
    .NET反编译之manager,base.AutoScaleMode修复
    无法加载Dll”ArcGISVersion.dll”:0x8007007E
    ArcEngine9.3迁移至ArcObject10.1
    地籍宗地出图(二)
    地籍宗地出图(一)
    CAD设置图层透明显示
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/7578231.html
Copyright © 2011-2022 走看看