POJ 3162 Walking Race (DFS + 线段树)

Walking Race
Time Limit: 10000MS   Memory Limit: 131072K
Total Submissions: 2315   Accepted: 536
Case Time Limit: 3000MS

Description

flymouse’s sister wc is very capable at sports and her favorite event is walking race. Chasing after the championship in an important competition, she comes to a training center to attend a training course. The center has Ncheck-points numbered 1 through N. Some pairs of check-points are directly connected by two-way paths. The check-points and the paths form exactly a tree-like structure. The course lasts N days. On the i-th day, wc picks check-point i as the starting point and chooses another check-point as the finishing point and walks along the only simple path between the two points for the day’s training. Her choice of finishing point will make it that the resulting path will be the longest among those of all possible choices.

After every day’s training, flymouse will do a physical examination from which data will obtained and analyzed to help wc’s future training be better instructed. In order to make the results reliable, flymouse is not using data all from N days for analysis. flymouse’s model for analysis requires data from a series of consecutive days during which the difference between the longest and the shortest distances wc walks cannot exceed a bound M. The longer the series is, the more accurate the results are. flymouse wants to know the number of days in such a longest series. Can you do the job for him?

Input

The input contains a single test case. The test case starts with a line containing the integers N (N ≤ 106) and M (M < 109). Then follow N − 1 lines, each containing two integers fi and di (i = 1, 2, …, N − 1), meaning the check-points i + 1 and fi are connected by a path of length di.

Output

Output one line with only the desired number of days in the longest series.

Sample Input

3 2
1 1
1 3

Sample Output

3

Hint

Explanation for the sample:

There are three check-points. Two paths of lengths 1 and 3 connect check-points 2 and 3 to check-point 1. The three paths along with wc walks are 1-3, 2-1-3 and 3-1-2. And their lengths are 3, 4 and 4. Therefore data from all three days can be used for analysis.

Source



DFS + 线段树



#include <iostream>
#include <cstdio>
#include <climits>
#include <algorithm>
using namespace std;

const int maxn=1000010;

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;

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,w0;
	for(int i=2;i<=n;i++){
		scanf("%d%d",&x,&w0);
		addedge(i,x,w0);
		addedge(x,i,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;
}

struct node{
	int l,r,minc,maxc;
}a[4*maxn];

void build(int l,int r,int k){
	a[k].l=l;
	a[k].r=r;
	if(l<r){
		int mid=(l+r)/2;
		build(l,mid,2*k);
		build(mid+1,r,2*k+1);
		a[k].maxc=max(a[2*k].maxc,a[2*k+1].maxc);
		a[k].minc=min(a[2*k].minc,a[2*k+1].minc);
	}else{
		a[k].maxc=d[l];
		a[k].minc=d[l];
	}
}

void query(int l,int r,int k){
	if(l<=a[k].l && a[k].r<=r){
		//cout<<a[k].maxc<<" "<<a[k].minc<<endl;
		mx=max(mx,a[k].maxc);
		mn=min(mn,a[k].minc);
	}else{
		int mid=(a[k].l+a[k].r)/2;
		if(r<=mid) query(l,r,2*k);
		else if(l>=mid+1) query(l,r,2*k+1);
		else{
			query(l,mid,2*k);
			query(mid+1,r,2*k+1);
		}
	}
}

void solve2(){
	build(1,n,1);
	int be=1,en=1,ans=0;
	while(en<=n){
		mx=0,mn=INT_MAX;
		query(be,en,1);
		if(mx-mn<=m){
			ans=max(en-be+1,ans);
			en++;
		}else be++;
	}
	cout<<ans<<endl;
}

void computing(){
	solve1();
	solve2(); 
}

int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		initial();
		input();
		computing();
	}
	return 0;
}



用RMQ 不过,会卡内存

posted @ 2013-08-19 12:12  炒饭君  阅读(195)  评论(0编辑  收藏  举报