POJ 3162(TreeDP+单调队列)

                                                                                                        Walking Race
Time Limit: 10000MS Memory Limit: 131072K
Total Submissions: 1851 Accepted: 424
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 N check-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.


这题不难,就是麻烦。

第一问求树上距离最远的点,用TreeDP,HDU上有一道P2196类似。

第二问是求最长的一个任意元素差不大于M的区间,网上不少人用的是线段树。但在梁神的启发下才发现可以用两个单调队列,一个维护最大值,一个维护最小值,两个指针表示当前区间。

PS:以前老爱用VECTOR存边,方便是挺方便,就是clear()太费时间了。


/*
 * =====================================================================================
 *
 *       Filename:  POJ_P3162.cpp
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  2012年08月03日 10时39分56秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Mad13 (),
 *   Organization:
 *
 * =====================================================================================
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>

#define Mid(a) ((a)>>1)
#define R(a) (((a)<<1)+1)
#define L(a) ((a)<<1)
#define maxn 1500000
#define Clear(a,b) memset(a,b,sizeof(a))

using namespace std;

int Max(int a,int b)
{
  if (a > b) return a;
  return b;
}

int Min(int a,int b)
{
  if (a > b) return b;
  return a;
}

struct edge{
  int st,sum,id,len;
  };

int n;
int q_min[maxn],q_max[maxn];
int dis[maxn],dis2[maxn];
bool vis[maxn];
edge links[maxn*2];
int star[maxn];

bool cmp(edge a,edge b)
{
  if (a.st != b.st) return a.st < b.st;
  return a.id < b.id;
}

void dfs(int k)
{
  //int m = links[k].size(),i;
  int i;
  for(i = star[k];i < star[k+1];i++)
     if (vis[links[i].id] == 0){
       vis[links[i].id] = 1;
       dfs(links[i].id);
       links[i].sum = dis[links[i].id]+links[i].len;
       dis[k] = Max(dis[k],links[i].sum);
       }
  return ;
}

void dfs2(int fa,int son)
{
  int maxx = 0,i;
  if (vis[son] == 1) return ;
  vis[son] = 1;

  for(i = star[fa];i < star[fa+1];i++)
    if (links[i].id != son)
      maxx = Max(maxx,links[i].sum);

  //m = links[son].size();
  for(i = star[son];i < star[son+1];i++)
    if (links[i].id == fa) {
      links[i].sum = maxx+links[i].len;
      break;
      }

  for(i = star[son];i < star[son+1];i++) {
    dis2[son] = Max(dis2[son],links[i].sum);
    dfs2(son,links[i].id);
    }
  return ;
}

int CountDis()
{
  int a,b,i;
  Clear(dis,0);
  Clear(dis2,0);
  //for(i = 1;i <= n;i++) links[i].clear();

  for(i = 2;i <= n;i++) {
    scanf("%d%d",&a,&b);
    links[i*2-2].st = i;
    links[i*2-2].id = a;
    links[i*2-2].sum = 0;
    links[i*2-2].len = b;
    //
    links[i*2-3].st = a;
    links[i*2-3].id = i;
    links[i*2-3].sum = 0;
    links[i*2-3].len = b;
    }
    //printf("OK\n");
  sort(links+1,links+n*2-1,cmp);
  for(i = 1;i <= n*2-2;i++)
    if (star[links[i].st] == 0) star[links[i].st] = i;
  star[n+1] = n*2+1;
  for(i = n;i >= 1;i--)
    if (star[i] == 0) star[i] = star[i+1];
  //for(i = 1;i <= n*2;i++) printf("%d %d %d %d\n",links[i].st,links[i].id,links[i].len,links[i].sum);
  //printf("\n");
  //for(i = 1;i <= n;i++) printf("%d ",star[i]);
  //printf("\n");
  Clear(vis,0);
  vis[1] = 1;
  dfs(1);
  //m = links[1].size();
  for(i = star[1];i < star[2];i++)
    dis2[1] = Max(dis[1],links[i].sum);
  //for(i = 1;i <= n;i++) printf("xx%d\n",dis[i]);
  Clear(vis,0);
  vis[1] = 1;
  for(i = star[1];i < star[2];i++) {
    dfs2(1,links[i].id);
    }
  return 0;
}

int work()
{
  int m;
  if (scanf("%d%d",&n,&m) == EOF) return 0 ;
  CountDis();
  //for(i = 1;i <= n;i++) printf("%d\n",dis2[i]);
  int h = 1,t = 1,ans = 1,t1 = 0,h1 = 0,t2 = 0,h2 = 0;
  Clear(q_min,0);
  Clear(q_max,0);
  q_min[t2++] = dis2[1];
  q_max[t1++] = dis2[1];
  //for(i = 1;i <= n;i++) printf("%d\n",dis2[i]);
  while(t < n){
    //printf("%d %d\n",h,t);
    if ((q_max[h1] - q_min[h2]) <= m) {
      t++;
      int tmp = dis2[t];
      while(h1 < t1 && q_max[t1-1] < tmp) t1--;
      q_max[t1++] = tmp;
      while(h2 < t2 && q_min[t2-1] > tmp) t2--;
      q_min[t2++] = tmp;
      }
    else {
      int tmp = dis2[h++];
      if (q_max[h1] == tmp) h1++;
      if (q_min[h2] == tmp) h2++;
      }
      if ((q_max[h1] - q_min[h2]) <= m && (t - h + 1) > ans) ans =  t - h + 1;
    }
  printf("%d\n",ans);
  return 1;
}

int main()
{
  while(work());
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值