Bob’s Race
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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)
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
题意:给你一个树,求每个点的最远距离,并且最最大的区间长度小于等于q;
思路:1:树的直径
2:rmq的st表;
3:尺取;
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; struct is { int v,w,nex; }edge[N]; int head[N],edg; int node1,node2,deep; int dis[N],num[N]; int pos[N]; void init() { memset(num,0,sizeof(num)); memset(head,-1,sizeof(head)); memset(dis,0,sizeof(dis)); edg=0; deep=0; } void add(int u,int v,int w) { edg++; edge[edg].v=v; edge[edg].w=w; edge[edg].nex=head[u]; head[u]=edg; } void dfs(int u,int fa,int val,int &node) { dis[u]=max(dis[u],val); if(val>deep) { deep=val; node=u; } for(int i=head[u];i!=-1;i=edge[i].nex) { int v=edge[i].v; int w=edge[i].w; if(v==fa)continue; dfs(v,u,val+w,node); } } int dpi[N][30]; int dpa[N][30]; int minn(int x,int y) { return num[x]<=num[y]?x:y; } void rmqi(int len) { for(int i=0; i<len; i++) dpi[i][0]=i; for(int j=1; (1<<j)<len; j++) for(int i=0; i+(1<<j)-1<len; i++) dpi[i][j]=minn(dpi[i][j-1],dpi[i+(1<<(j-1))][j-1]); } int queryi(int l,int r) { int x=pos[r-l+1]; return minn(dpi[l][x],dpi[r-(1<<x)+1][x]); } int maxx(int x,int y) { return num[x]>=num[y]?x:y; } void rmqa(int len) { for(int i=0; i<len; i++) dpa[i][0]=i; for(int j=1; (1<<j)<len; j++) for(int i=0; i+(1<<j)-1<len; i++) dpa[i][j]=maxx(dpa[i][j-1],dpa[i+(1<<(j-1))][j-1]); } int querya(int l,int r) { int x=pos[r-l+1]; return maxx(dpa[l][x],dpa[r-(1<<x)+1][x]); } int n,m; int main() { pos[0]=-1; for(int i=1;i<100000;i++) pos[i]=pos[i>>1]+1; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; init(); for(int i=1;i<n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } dfs(1,-1,0,node1); deep=0; dfs(node1,-1,0,node2); dfs(node2,-1,0,node1); for(int i=1;i<=n;i++) num[i]=max(dis[i],num[i]); rmqi(n+1); rmqa(n+1); while(m--) { int z; scanf("%d",&z); int l=1,r=1,ans=0; while(1) { while(num[querya(l,r)]-num[queryi(l,r)]<=z&&r<=n)r++; ans=max(ans,r-l); if(r>n) break; l++; } printf("%d ",ans); } } return 0; }