题目链接:戳这里
Bob’s Race
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3884 Accepted Submission(s): 1235
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
题解:首先考虑树的直径的求法。
step 1、从任意一个点开始dfs,到达最远的点一定是树的直径的一个端点,并记录此端点。
step2、从上一步记录的端点开始dfs,到达最远的点即为树的直径的另一端点。
因此,由step1可知,任意一点能够到达的最远节点一定是树的直径的两个端点之一,所以先求出树的直径,再记录每个点到两个端点的距离,据此可以求得val[i]=max(dis[0][i],dis[1][i]);
考虑第二位,因为牵扯到区间最值并且没有修改,因此可以预处理rmq来查询。
查询方法:枚举右端点,如果左端点不满足条件则只能向右走,满足单调性,所以每次查询复杂度为O(n)
综上,此题复杂度为O(n)+O(nlogn)+O(nm)。
记得手动扩栈防止RE。
代码:
#include<bits/stdc++.h>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 50005
using namespace std;
typedef long long LL;
int read()
{
char c;int sum=0,f=1;c=getchar();
while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0' && c<='9'){sum=sum*10+c-'0';c=getchar();}
return sum*f;
}
int n,m,q;
struct node{
int to,val;
};
vector<node> edge[maxn];
int val[maxn];
int dis[2][maxn];
int st,ed,ans,id,res;
int maxa[maxn][32],mina[maxn][32],ln[maxn],l;
void init()
{
ans=-1;
for(int i=1;i<=n;i++)
edge[i].clear();
}
void dfs(int x,int fa,int sum)
{
if(sum>ans)
{
ans=sum;
st=x;
}
int lens=edge[x].size();
for(int i=0;i<lens;i++)
{
node nex=edge[x][i];
if(nex.to==fa) continue;
dfs(nex.to,x,sum+nex.val);
}
}
void dfs2(int x,int fa,int sum,int f)
{
dis[f][x]=sum;
int lens=edge[x].size();
for(int i=0;i<lens;i++)
{
node nex=edge[x][i];
if(nex.to==fa) continue;
dfs2(nex.to,x,sum+nex.val,f);
}
}
void rmq()
{
for(int i=1;i<=n;i++)
maxa[i][0]=mina[i][0]=val[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)<=n+1;i++)
{
maxa[i][j]=max(maxa[i][j-1],maxa[i+(1<<j-1)][j-1]);
mina[i][j]=min(mina[i][j-1],mina[i+(1<<j-1)][j-1]);
}
}
int query(int l,int r)
{
int k=ln[r-l+1];
return max(maxa[l][k],maxa[r-(1<<k)+1][k])-min(mina[l][k],mina[r-(1<<k)+1][k]);
}
int main()
{
for(int i=1;i<=50000;i++)
ln[i]=(1<<l+1==i)?++l:l;
while(n=read(),m=read(),n&&m)
{
init();
for(int i=1;i<n;i++)
{
int u=read(),v=read(),w=read();
edge[u].push_back((node){v,w});
edge[v].push_back((node){u,w});
}
dfs(1,0,0);
ed=st;ans=-1;
dfs(st,0,0);
dfs2(st,0,0,0);dfs2(ed,0,0,1);
for(int i=1;i<=n;i++)
val[i]=max(dis[1][i],dis[0][i]);
rmq();
while(m--)
{
q=read();
ans=id=1;
for(int i=1;i<=n;i++)
{
while(id<=i)
{
res=query(id,i);
if(res>q) id++;
else break;
}
ans=max(ans,i-id+1);
}
printf("%d\n",ans);
}
}
return 0;
}