Bob’s Race
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2753 Accepted Submission(s): 888
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
Recommend
题目大意
给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离Ma[i],有m个询问,每个询问有个q,求最大的连续节点区间长度ans,使得该区间内最大的M[i]和最小的M[j]之差不超过q。
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
//#define max(a,b) (a>b?a:b)
//#define min(a,b) (a>b?b:a)
__int64 maxv[50050][30],minv[50050][30],d[50050],maxn,d1[50050],d2[50050],dis[50050],lg[50050];
int n,m,cnt,s,head[50050],vis[50050];
struct s
{
int u,v,w,next;
}edge[50050*2];
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
__int64 max(__int64 a,__int64 b)
{
if(a>b)
return a;
return b;
}
__int64 min(__int64 a,__int64 b)
{
if(a>b)
return b;
return a;
}
void bfs(int u)
{
int i;
queue<int>q;
memset(vis,0,sizeof(vis));
vis[u]=1;
dis[u]=0;
s=u;
maxn=1;
q.push(u);
while(!q.empty())
{
int u=q.front();
q.pop();
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
vis[v]=1;
dis[v]=dis[u]+edge[i].w;
q.push(v);
if(dis[v]>maxn)
{
maxn=dis[v];
s=v;
}
}
}
}
}
void init()
{
int i,j,k;
lg[1]=0;
for(i=2;i<=50000;i++)
{
lg[i]=lg[i>>1]+1;
}
for(i=1;i<=n;i++)
{
minv[i][0]=maxv[i][0]=d[i];
}
for(j=1;(1<<j)<=n;j++)
{
for(k=0;k+(1<<j)-1<=n;k++)
{
minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
maxv[k][j]=max(maxv[k][j-1],maxv[k+(1<<(j-1))][j-1]);
}
}
}
__int64 q_max(int l,int r)
{
//int k=(int)(log((double)(r-l+1))/log(2.0));
int k=lg[r-l+1];
return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
}
__int64 q_min(int l,int r)
{
//int k=(int)(log((double)(r-l+1))/log(2.0));
int k=lg[r-l+1];
return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n||m)
{
int i;
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
bfs(1);
bfs(s);
for(i=1;i<=n;i++)
{
d1[i]=dis[i];
}
bfs(s);
for(i=1;i<=n;i++)
{
d2[i]=dis[i];
d[i]=max(d1[i],d2[i]);
}
/*for(i=1;i<=n;i++)
{
printf("+++++%d\n",d[i]);
}*/
init();
while(m--)
{
__int64 q;
scanf("%I64d",&q);
int p=1,len=0;
for(i=1;i<=n;i++)
{
if(q_max(p,i)-q_min(p,i)>q&&p<i)
{
p++;
}
if((i-p+1)>len)
len=i-p+1;
}
printf("%d\n",len);
}
}
}
看到别人用dfs求的树的直径,用了800多ms,比我的快一半了,收藏一下
ac代码http://www.xuebuyuan.com/zh-tw/2118373.html
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 50005
#define MAXN 100005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
int n,m,ans,cnt,tot,flag;
int len,st,ed; // 直徑 起點 終點
bool vis[maxn];
int head[maxn],val[maxn],dist[maxn][2];
int f[maxn][20],g[maxn][20],lg[maxn]; //第二維為二進位
struct Node
{
int v,w,next;
} edge[MAXN];
void addedge(int u,int v,int w)
{
cnt++;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
}
void dfs(int u,int fa,int sum)
{
if(sum>len){ len=sum;st=u; }
int i,j,v;
for(i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa)
{
dfs(v,u,sum+edge[i].w);
}
}
}
void dfs1(int u,int fa,int k,int sum)
{
dist[u][k]=sum;
int i,j,v;
for(i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa) dfs1(v,u,k,sum+edge[i].w);
}
}
void init_rmq() // 預處理 O(n*log(n))
{
int i,j;
for(i=1;i<=n;i++)
{
f[i][0]=g[i][0]=val[i];
}
for(j=1;(1<<j)<=n;j++)
{
for(i=1;i+j-1<=n;i++)
{
if((i+(1<<(j-1))<=n))
{
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); //避免越界
g[i][j]=min(g[i][j-1],g[i+(1<<(j-1))][j-1]);
}
else
{
f[i][j]=f[i][j-1];
g[i][j]=g[i][j-1];
}
}
}
}
int query_rmq(int l,int r)
{
int k=lg[r-l+1];
return max(f[l][k],f[r-(1<<k)+1][k])-min(g[l][k],g[r-(1<<k)+1][k]);
}
int main()
{
int i,j,t;
lg[1]=0;
for(i=2;i<=50000;i++)
{
lg[i]=lg[i>>1]+1;
}
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break ;
cnt=0;
memset(head,0,sizeof(head));
int u,v,w;
for(i=1;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
len=0;
dfs(1,0,0);
ed=st;
len=0;
dfs(st,0,0);
dfs1(st,0,0,0);
dfs1(ed,0,1,0);
for(i=1;i<=n;i++)
{
val[i]=max(dist[i][0],dist[i][1]);
// printf("i:%d val:%d\n",i,val[i]);
}
init_rmq();
int q,le,ri,mid,res,id;
while(m--)
{
scanf("%d",&q);
ans=id=1;
for(i=1;i<=n;i++)
{
while(id<=i)
{
res=query_rmq(id,i);
if(res>q) id++;
else break ;
}
ans=max(ans,i-id+1);
}
printf("%d\n",ans);
}
}
return 0;
}