poj3162 Walking Race
来源:互联网 发布:国家c语言考试时间 编辑:程序博客网 时间:2024/05/29 11:00
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
#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;}
- poj3162 Walking Race
- POJ3162 Walking Race
- poj 3162 Walking Race
- poj 3162 Walking Race
- POJ 3162 Walking Race
- POJ 3162 Walking Race 笔记
- POJ 3162(TreeDP+单调队列)Walking Race
- POJ 3162 Walking Race (DFS + 线段树)
- POJ 3162 Walking Race 树形DP
- POJ 3162 Walking Race (树形DP)
- POJ 3162 - Walking Race(树形DP)
- POJ 3162 Walking Race (单调队列)@
- POJ 3162 Walking Race(树形DP+单调队列)
- POJ 3162 Walking Race(树形DP + 单调队列)
- POJ 3162 Walking Race TreeDP+双单调队列 ★
- poj 3162 walking race 树形dp 求符合条件区间
- POJ 3162Walking Race 树的直径加单调队列
- 解题报告:POJ 3162 Walking Race 树型DP+单调RMQ
- hibernate之constrained详解
- 致那些迷茫与不知前途如何的IT从业者
- TFS
- matplotlib画直方图(1)
- xargs 的用法
- poj3162 Walking Race
- 中国云计算现状:糟糕的公有云服务阻碍了中国云计算的进一步发展
- poj1016-Numbers That Count
- 南洋理工 55 懒省事的小明
- SystemC Modules
- 往死里写——SDO处理2-2
- Java基础知识_自动装箱/享元/枚举
- 《数据通信与网络》笔记--广域网SONET/SDH
- <s:property>的用法