poj3162 Walking Race

来源:互联网 发布:国家c语言考试时间 编辑:程序博客网 时间:2024/05/29 11:00
Walking Race
Time Limit: 10000MS Memory Limit: 131072KTotal Submissions: 2157 Accepted: 514Case 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 21 11 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

POJ Monthly--2006.12.31, flymouse

要做这个题得先做前两个博客里面的题(当然大神就不要来吐槽了),就是hdu2196和poj2823这两题,一个用到了树形dp而且是双搜的,另一个用到了优先队列,而这一题把这两个都用到了。这个题后半部分是我在网上借鉴的,快写完时才发现原来作者用的是近乎暴力的方法做的,早知道就自己写了,这个后半部分应该是可以用优先队列的,只不过和前面的稍有不同,如果用优先队列那时间肯定可以提升很多
Memory: 47380 KBTime: 7594 MSLanguage: C++Result: Accepted
#include<vector>#include<iostream>using namespace std;#define N 1000003struct 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]].todownf[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;}