POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)


 

Bob’s Race
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 378   Accepted: 119

 

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) 

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



题目大意:告诉n,m , 表示一棵树n个节点,接下来n-1条边将n个节点,m表示m组查询q,查询最长的连续区间差值不超过q的长度。

解体思路:与我的    poj 3162 Walking Race (DFS + 线段树)  有点类似,但是,比那个复杂,不同在那个只有一次查询,这次m次查询,那个效率 O( n*lgn ),如果用那个方法,这次也就是  O(m* n*lgn )的效率,实验发现超时了,最后用了 rmq算法代替了线段树,用rmq预处理好,查询每次区间的效率为 1 而不是 lgn ,所以效率变为   O(m* n),花了 1秒左右AC。 

 

#include <iostream>

#include <cstdio>

#include <climits>

#include <map>

#include <vector>

#include <algorithm>

using namespace std;



const int maxn=100010;



struct edge{

	int u,v,w;

	int next;

	edge(int u0=0,int v0=0,int w0=0){ u=u0;v=v0;w=w0;}

}e[maxn*2];



int n,m,cnt,head[maxn],d[maxn],dx[maxn],dy[maxn],qmin[maxn],qmax[maxn],mx,mn;

int maxsum[maxn][20],minsum[maxn][20],flog[maxn];



void initial(){

	cnt=0;

	for(int i=0;i<=n;i++) head[i]=-1;

}



void addedge(int u,int v,int w){

	e[cnt]=edge(u,v,w);

	e[cnt].next=head[u];

	head[u]=cnt++;

}



void input(){

	int x,y,w0;

	for(int i=2;i<=n;i++){

		scanf("%d%d%d",&x,&y,&w0);

		addedge(x,y,w0);

		addedge(y,x,w0);

	}

}



void dfs(int u,int fa,int dis,int *d){

	for(int i=head[u];i!=-1;i=e[i].next){

		int v=e[i].v,w=e[i].w;

		if(v!=fa) dfs(v,u,d[v]=dis+w,d);

	}

}



void solve1(){

	int x=1,y=1;

	dfs(1,-1,d[1]=0,d);

	for(int i=1;i<=n;i++) if(d[x]<d[i]) x=i;

	dfs(x,-1,dx[x]=0,dx);

	for(int i=1;i<=n;i++) if(dx[y]<dx[i]) y=i;

	dfs(y,-1,dy[y]=0,dy);

	for(int i=1;i<=n;i++) d[i]=max(dx[i],dy[i]);

	//for(int i=1;i<=n;i++) cout<<"dis["<<i<<"]:"<<d[i]<<endl;

}



void getrmq(){

	int r=2,cnt=0;

	for(int i=1;i<=n;i++){

		if(i<r) flog[i]=cnt;

		else{

			flog[i]=++cnt;

			r=r<<1;

		}

	}

	for(int i=1;i<=n;i++){

		maxsum[i][0]=d[i];

		minsum[i][0]=d[i];

	}

	for(int j=1;j<=flog[n];j++)

	for(int i=1;i<=n;i++){

		if(i+(1<<j)-1<=n){

			maxsum[i][j]=max(maxsum[i][j-1],maxsum[i+(1<<(j-1))][j-1]);

			minsum[i][j]=min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);

		}

	}

}



int getmin(int l,int r){

	int x=flog[r-l+1];

	return min(minsum[l][x],minsum[r-(1<<x)+1][x]);

}



int getmax(int l,int r){

	int x=flog[r-l+1];

	return max(maxsum[l][x],maxsum[r-(1<<x)+1][x]);

}



void solve2(){

	int be=1,en=1,ans=1,q=1;

	map <int,int> mp;

	vector<int> v;

	map <int,int>::iterator it;

	for(int i=0;i<m;i++){

		scanf("%d",&q);

		mp[q]=0;

		v.push_back(q);

	}

	for(it=mp.begin();it!=mp.end();it++){

		int be=1,en=be+ans-1;

		while(en<=n){

			mn=getmin(be,en),mx=getmax(be,en);

			if(mx-mn<=(it->first)){

				ans=max(en-be+1,ans);

				en++;

			}else{

				be++;

				en=max(en,be+ans-1);

			}

		}

		it->second=ans;

	}

	for(int i=0;i<m;i++) printf("%d\n",mp[v[i]]);

}



void computing(){

	solve1();

	getrmq();

	solve2(); 

}



int main(){

	while(scanf("%d%d",&n,&m)!=EOF && (n||m) ){

		initial();

		input();

		computing();

	}

	return 0;

}


 

 

 

你可能感兴趣的:(poj)