POJ 3162(TreeDP+单调队列)Walking Race

Walking Race
Time Limit: 10000MS Memory Limit: 131072K
Total Submissions: 2157 Accepted: 514
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

#include<vector>
#include<iostream>
using namespace std;
#define N 1000003

struct node{
    int to,cap;//to是当前的值,cap是与父亲节点的权值
}a;

vector<node>vt[N];

int n,dp[N],downf[N],downs[N],vis[N],in,x,first1,last1,first2,last2,f,t;
int qmax[1000003],qmin[1000003],index1[1000003],index2[1000003],r[1000003];

int rmax(int &i,int &j)
{
	return i>j?i:j;
}

void dfs1(int x)//从上往下,递归
{
	int len,i,j,max1=-1,flag1=-1,flag2=-1;
	len=vt[x].size();
	if(len==0)
		return ;
	if(downf[x])//重要
		return ;
	for(i=0;i<len;i++)
	{
		j=vt[x][i].to;
		dfs1(j);
		if(max1<downf[j]+vt[x][i].cap)
		{
			max1=downf[j]+vt[x][i].cap;
			flag1=i;
		}
	}
	//printf(" %d~%d ",x,max);
	vis[x]=flag1;//记录最远边,即vt[x][vis[x]].to
	downf[x]=max1;
	max1=-1;
	for(i=0;i<len;i++)
	{
		j=vt[x][i].to;
		//dfs1(j);//此时各个点的最长距离已经知道,所以就不需要递归了
		if(i!=flag1&&max1<downf[j]+vt[x][i].cap)//保证不重边
		{
			max1=downf[j]+vt[x][i].cap;
			flag2=i;
		}
	}
	if(flag2!=-1)
		downs[x]=max1;//第二长
}

void dfs2(int x)//从上往下
{
	int len=vt[x].size(),i,j;
	if(len==0)
		return ;
	for(i=0;i<len;i++)
	{
		j=vt[x][i].to;
		if(i==vis[x])//j在父亲节点的最长路径上
			dp[j]=rmax(downs[x],dp[x])+vt[x][i].cap;//选择第二长路径
		else dp[j]=rmax(downf[x],dp[x])+vt[x][i].cap;//j不在最长路径上,选第一长路径
		dfs2(j);
	}
}

int main()
{
	int i,j,k,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=0;i<n;i++)
			vt[i].clear();
		for(i=2;i<=n;i++)
		{
			scanf("%d%d",&j,&a.cap);
			a.to=i;
			vt[j].push_back(a);//在父节点后记录子节点
		}
		memset(dp,0,sizeof(dp));
        memset(downf,0,sizeof(downf));
        memset(downs,0,sizeof(downs));
		dfs1(1);
		dfs2(1);
		for(i=1;i<=n;i++)
		{
			r[i]=rmax(downf[i],dp[i]);
			//printf("r%d ",r[i]);
		}
		first1=last1=1;
		first2=last2=1;
		index1[last1++]=1;
		qmax[1]=r[1];
		qmin[1]=r[1];
		index2[last2++]=1;
		i=1;
		f=1;
		while(i<n)
		{
			if(qmax[first1]-qmin[first2]<=m)
			{
				i++;//记录走的点位置
				while(first1<last1&&qmax[last1-1]<r[i])//建立单调队列
				last1--;
				qmax[last1++]=r[i];
				while(first2<last2&&qmin[last2-1]>r[i])
					last2--;
				qmin[last2++]=r[i];
			}
			else 
			{
				if(qmax[first1]==r[f])
					first1++;
				if(qmin[first2]==r[f])
					first2++;
				f++;//记录最前面的点.写到这时突然明白了这后面的就是暴力啊
			}
			if(qmax[first1]-qmin[first2]<=m&&x<i-f+1)
				x=i-f+1;
		}
		printf("%d\n",x);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值