还没有学过RMQ,所以只能用会的单调队列做。
Bob’s Race
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1358 Accepted Submission(s): 441
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
lcy
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <math.h> #include <map> #include <queue> #include <sstream> #include <iostream> using namespace std; #define INF 0x3fffffff #define N 50050 int n,m; struct node { int to,next,w; }edge[2*N]; struct qq { int time,key; }que[N],que1[N]; int cnt,pre[N]; int g[N]; int mx,mi; int dp[N][2];//记录一次dfs中子树的最大值和次大值 int save[N]; //记录每个点到相邻点最大路程的id号 void add_edge(int u,int v,int w) { edge[cnt].to=v; edge[cnt].w=w; edge[cnt].next=pre[u]; pre[u]=cnt++; } int dfs(int s,int path) { for(int p=pre[s];p!=-1;p=edge[p].next) { int v=edge[p].to; if(v!=path) { int tmp=dfs(v,s)+edge[p].w; if(tmp>=dp[s][0]) { dp[s][1]=dp[s][0]; dp[s][0]=tmp; save[s]=v; } else { if(tmp>dp[s][1]) dp[s][1]=tmp; } } } return dp[s][0]; } void dfs1(int s,int sum,int path) { if(sum>=dp[s][0]) { dp[s][1]=dp[s][0]; dp[s][0]=sum; save[s]=path; } else if(sum>dp[s][1]) dp[s][1]=sum; g[s]=dp[s][0]; for(int p=pre[s];p!=-1;p=edge[p].next) { int v=edge[p].to; if(v!=path) { if(save[s]==v) dfs1(v,dp[s][1]+edge[p].w,s); else dfs1(v,dp[s][0]+edge[p].w,s); } } } int main() { //freopen("//home//chen//Desktop//ACM//in.text","r",stdin); //freopen("//home//chen//Desktop//ACM//out.text","w",stdout); while(scanf("%d%d",&n,&m)&&(m+n)) { cnt=0; memset(pre,-1,sizeof(pre)); for(int i=1;i<n;i++) { int x,y,key; scanf("%d%d%d",&x,&y,&key); add_edge(x,y,key); add_edge(y,x,key); } memset(dp,0,sizeof(dp)); dfs(1,0);//记录好 dfs1(1,0,1);//这样就可以求出每个出发的最长路 /* for(int i=1;i<=n;i++) printf("%d ",g[i]); printf(" "); */ int qf,qd; int qf1,qd1; for(int ii=0;ii<m;ii++) { int ans=1,lim; scanf("%d",&lim); mx=g[1]; mi=g[1]; qf=qd=qf1=qd1=0; int k=0; int tans=1; que[qf].key=g[1]; que[qf].time=1; qf++; que1[qf1].key=g[1]; que1[qf1].time=1; qf1++; for(int i=2;i<=n;i++) { while(qf>qd&&que[qf-1].key<=g[i]) qf--; que[qf].time=i; que[qf].key=g[i]; qf++; ////// while(qf1>qd1&&que1[qf1-1].key>=g[i]) qf1--; que1[qf1].time=i; que1[qf1].key=g[i]; qf1++; mx=que[qd].key; mi=que1[qd1].key; tans++; if(mx-mi<=lim) { if(tans>ans) ans=tans; } else { while(mx-mi>lim) { k++; tans--; while(qf>qd&&que[qd].time<=k) qd++; while(qf1>qd1&&que1[qd1].time<=k) qd1++; mx=que[qd].key; mi=que1[qd1].key; } } } printf("%d ",ans); //单调队列,重要的是维护一个单调的队列,然后及时删除掉过时的元素。就可以保证在一个区间能找到最大值。 } } return 0; }