POJ

来源:互联网 发布:php 教程 编辑:程序博客网 时间:2024/06/05 03:16

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 51 2 32 3 44 5 33 4 2123450 0
Sample Output
13335



题意:


n个点的树,每个点有一个人,每个人会跑到离自己初始点距离最远的点上,
这个距离为distance[i]。给你m个查询,对于每个查询Q,找一段连续编号的人,
比如[left,right],满足 max( distance[i] i∈[left,right] ) – min( distance[i] i∈[left,right] ) ≤ Q,
并且使得length=right-left+1要最大,求这个最大的length


#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>//#include <bits/stdc++.h>using namespace std;const int N = 1e5+10;typedef long long LL;int a[N], head[N], q1[N], q2[N], dp[N][3], dp1[N], id[N];struct node{    int next, to, w;} p[N*2];int cnt, n, m;void add(int u,int v,int w){    p[cnt].to=v,p[cnt].next=head[u],p[cnt].w=w;    head[u]=cnt++;    return ;}void dfs1(int u,int pre){    dp[u][0]=dp[u][1]=0;    for(int i=head[u]; i!=-1; i=p[i].next)    {        int v=p[i].to;        if(v==pre) continue;        dfs1(v,u);        if(dp[v][0]+p[i].w>=dp[u][0])        {            dp[u][1]=dp[u][0],id[u]=v;            dp[u][0]=dp[v][0]+p[i].w;        }        else if(dp[v][0]+p[i].w>dp[u][1])  dp[u][1]=dp[v][1]+p[i].w;    }    return ;}void dfs2(int u,int pre,int sum){    for(int i=head[u]; i!=-1; i=p[i].next)    {        int v=p[i].to;        if(v==pre) continue;        if(v==id[u]) dfs2(v,u,max(sum,dp[u][1])+p[i].w);        else dfs2(v,u,max(sum,dp[u][0])+p[i].w);    }    a[u]=max(sum,dp[u][0]);    return ;}int judge(int x){    if(dp1[x]!=-1) return dp1[x];    int s1=1,s2=1,t1=0,t2=0, ans=999999999;    for(int i=1;i<=n;i++)    {        while(s1<=t1 && a[q1[t1]]<=a[i]) t1--;        q1[++t1]=i;        while(s1<=t1 && i-x>=q1[s1]) s1++;                while(s2<=t2 && a[q2[t2]]>=a[i]) t2--;        q2[++t2]=i;        while(s2<=t2 && i-x>=q2[s2]) s2++;        if(i>=x) ans=min(ans,a[q1[s1]]-a[q2[s2]]);    }    dp1[x]=ans;    return ans;}int vis[N];int main(){    while(scanf("%d %d", &n, &m),n||m)    {        memset(head,-1,sizeof(head));        memset(dp1,-1,sizeof(dp1));        cnt=0;        memset(vis,0,sizeof(vis));        for(int i=1; i<n; i++)        {            int x, y, z;            scanf("%d %d %d", &x, &y, &z);            add(x,y,z);            add(y,x,z);            vis[x]++,vis[y]++;        }        int s=0;        for(int i=1;i<=n;i++)        {            if(vis[i]==1)            {                s=i;                break;            }        }        dfs1(s,-1);        int h=0;        h++;        dfs2(s,-1,0);        while(m--)        {            int x;            scanf("%d", &x);            int l=0, r=n, ans=1;            while(l<=r)            {                int mid=(l+r)/2;                if(judge(mid)<=x) ans=mid,l=mid+1;                else r=mid-1;            }            cout<<ans<<endl;        }    }    return 0;}