poj 3162(dfs+单调队列)
来源:互联网 发布:优化实践技能 编辑:程序博客网 时间:2024/05/20 11:24
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.
#include <iostream>#include <cstdio>using namespace std;const int maxn = 1000010;struct tree{ int l; int r; int mi; int ma;}a[4*maxn];struct edge{ int u , v ,f; edge(int a = 0, int b = 0, int c = 0){ u = a , v = b , f = c; }}e[3*maxn];int N , M , cnt;int head[maxn] , next[maxn];int t_minn , t_maxn , d[maxn],dx[maxn],dy[maxn];int q1[maxn] , q2[maxn];void add(int u , int v , int c){ e[cnt] = edge(u , v , c) , next[cnt] = head[u] , head[u] = cnt++; e[cnt] = edge(v, u , c) , next[cnt] = head[v] , head[v] = cnt++;}void initial(){ for(int i = 0;i < maxn;i++){ head[i] = -1; next[i] = -1; d[i] = 0; dx[i] = 0; dy[i] = 0; } cnt = 0;}void readcase(){ int v, f; for(int i = 0;i < N-1;i++){ scanf("%d%d" , &v , &f); add(i+2 , v , f); }}void dfs(int u,int fa,int dis,int *d){ for(int i=head[u];i!=-1;i=next[i]){ int v=e[i].v,f=e[i].f; if(v!=fa) dfs(v,u,d[v]=dis+f,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 computing(){ solve1(); int ans = 0; int l = 1; int r = 1; int head1 = 0, rear1 = 0 , head2 = 0, rear2 = 0; q1[0] = 1; q2[0] = 1; while(l <= r && r <= N){ while(d[q1[rear1]] <= d[r] && rear1 >= head1){ rear1--; } q1[++rear1] = r; while(d[q2[rear2]] >= d[r] && rear2 >= head2){ rear2--; } q2[++rear2] = r; if(d[q1[head1]] - d[q2[head2]] <= M){ ans = max(ans , r-l+1); r++; }else{ if(q1[head1] <= q2[head2]){ l = q1[head1++]; l++; }else{ l = q2[head2++]; l++; } } } cout << ans << endl;}int main(){ while(~scanf("%d%d" , &N ,&M)){ initial(); readcase(); computing(); } return 0;}
- poj 3162(dfs+单调队列)
- poj 3162 Walking Race (dfs+尺取法单调队列)★
- POJ 3162 Walking Race (单调队列)@
- POJ 3162(TreeDP+单调队列)
- poj 2823 (单调队列)
- poj 2823(单调队列)
- poj 2823(单调队列)
- POJ 2823(单调队列)
- POJ 2823(单调队列)
- POJ 3162 Walking Race(树形DP + 单调队列)
- POJ 3162(TreeDP+单调队列)Walking Race
- poj 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- Poj 2823 (单调队列裸题)
- poj 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window ( 单调队列 )
- POJ 2823 Sliding Window (单调队列)
- 『Asp.Net 组件』Asp.Net 服务器组件 内嵌JS:让自己的控件动起来
- 黑马程序员--Java数组
- Java 排序实现
- vsftp功能分析及虚拟用户服务
- Java通道方法写文件中缓冲区建立的原则
- poj 3162(dfs+单调队列)
- Uva 562 背包DP
- c++命名空间三种实现文件的区别
- ViewPager例子//水平滑动
- C++标准模板库分析之二
- 六款优秀的Linux Web缓存开源软
- Ubuntu linux中怎么查看自己的串口连接情况
- 原码、反码、补码、移码
- [Ext JS 4] 实战之Grid, Tree Gird 动态添加行