http://acm.hust.edu.cn/vjudge/problem/11552

http://blog.csdn.net/woshi250hua/article/details/7727677

题目大意:给定一张地图,它是一棵n个节点的树。mm爱跑步,mm要跑n天,每次都从一个结点开始跑步,每次都要跑到最远的那个结点,两天跑的最远距离有个差值,现在要从这n天里去若干天使得这些天的差值都小于m,问怎么取使得天数最多?n <= 100万,m <= 1亿。

先求每个点到其他点距离最小值

再求小于m最长的区间范围

如果掌握

hdu 2196 叶子节点最长距离

http://www.cnblogs.com/qlky/p/5774759.html

以及hdu 3530 区间和在一定范围内最大区间

http://www.cnblogs.com/qlky/p/5791485.html

那么这题太简单了,10分钟内肯定能AC

求最大区间用线段树(5000+MS)也能做,但单调队列快很多,2600MS

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1000000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m; int head[MAXN],vis[MAXN],ptr=; int mx[MAXN],mx2[MAXN],vx[MAXN],vx2[MAXN]; int p1[MAXN],p2[MAXN],ans; struct node{int y,next,val;}tree[MAXN<<]; void init()
{
mem(head,-);
mem(vis,);
ptr = ;
} void add(int son,int fa,int val)
{
tree[ptr].y=son;
tree[ptr].val=val;
tree[ptr].next=head[fa];
head[fa]=ptr++;
} void dfs(int root,int fa)
{
for(int i = head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(y==fa) continue;
dfs(y,root);
if(mx2[root]<mx[y]+tree[i].val)
{
vx2[root] = y;
mx2[root] = mx[y]+tree[i].val;
if(mx2[root]>mx[root])
{
swap(vx2[root],vx[root]);
swap(mx2[root],mx[root]);
}
}
}
} void dfs2(int root,int fa)
{
for(int i = head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(y==fa) continue;
if(y == vx[root])
{
if(mx2[root]+tree[i].val > mx2[y])
{
mx2[y] = mx2[root]+tree[i].val;
vx2[y] = root;
if(mx2[y]>mx[y])
{
swap(vx2[y],vx[y]);
swap(mx2[y],mx[y]);
}
}
}
else
{
if(mx[root]+tree[i].val > mx2[y])
{
mx2[y] = mx[root]+tree[i].val;
vx2[y] = root;
if(mx2[y]>mx[y])
{
swap(vx2[y],vx[y]);
swap(mx2[y],mx[y]);
}
}
}
dfs2(y,root);
}
} void getL()
{
int h1,h2,r1,r2,pre;
h1=h2=pre=;
r1=r2=ans=;
for(int i=;i<=n;i++)
{
while(h1<=r1 && mx[p1[r1]]>=mx[i]) r1--;
p1[++r1] = i;
while(h2<=r2 && mx[p2[r2]]<=mx[i]) r2--;
p2[++r2] = i;
while(h1<=r1 && h2<=r2 && mx[p2[h2]]-mx[p1[h1]]>m)
{
if(p1[h1]>p2[h2]) pre = p2[h2++]+;
else pre = p1[h1++]+;
}
if(h1<=r1 && h2<=r2 && mx[p2[h2]]>=mx[p1[h1]])
{
ans = max(ans,i-pre+);
}
}
} int main()
{
int i,j;
while(~sf("%d%d",&n,&m))
{
init();
for(i=;i<=n;i++)
{
int x,y,z;
sf("%d%d",&x,&y);
add(i,x,y);
add(x,i,y);
}
dfs(,);
dfs2(,);
/*
for(i=1;i<=n;i++) pf("%d ",mx[i]);
blank;
*/
getL();
pf("%d\n",ans);
}
}

poj 3162 树DP+单调队列的更多相关文章

  1. BZOJ1023:[SHOI2008]cactus仙人掌图(圆方树,DP,单调队列)

    Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus). 所谓简单回路就是指在图上不重复经过任何一个顶点 ...

  2. 1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP

    1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP 题意 用摄像机观察动物,有两个摄像机,一个可以放在奇数天,一个可以放在偶数天.摄像机在 ...

  3. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  4. bzoj 1171 并查集优化顺序枚举 | 线段树套单调队列

    详见vfleaking在discuss里的题解. 收获: 当我们要顺序枚举一个序列,并且跳过某些元素,那么我们可以用并查集将要跳过的元素合并到一起,这样当一长串元素需要跳过时,可以O(1)跳过. 暴力 ...

  5. (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列

    Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...

  6. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

  7. 习题:烽火传递(DP+单调队列)

    烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...

  8. 3622 假期(DP+单调队列优化)

    3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...

  9. POJ 3162 Walking Race(树形dp+单调队列 or 线段树)

    http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...

随机推荐

  1. PHP 符号

    注解符号: // 单行注解 /*      */    多行注解 引号的使用 ’   ’ 单引号,没有任何意义,不经任何处理直接拿过来; " "双引号,PHP动态处理然后输出,一般 ...

  2. React-Native App启动页制作(安卓端)

    原文地址:React-Native App启动页制作(安卓端) 这篇文章是根据开源项目react-native-splash-screen来写的.在使用react-native-link命令安装该包后 ...

  3. linux线程切换和进程切换

    进程切换分两步: 1.切换页目录以使用新的地址空间 2.切换内核栈和硬件上下文 对于linux来说,线程和进程的最大区别就在于地址空间,对于线程切换,第1步是不需要做的,第2是进程和线程切换都要做的. ...

  4. jquery发送请求

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 【ABP开发】:asp.net core 中使用mysql

    EntityFrameworkCore项目--Nuget包管理,卸载包: Microsoft.EntityFrameworkCore.SqlServer: EntityFrameworkCore项目和 ...

  6. nginx安装及高可用

    nginx的安装: server1: tar zxf nginx-1.14.0.tar.gz cd nginx-1.14.0/src/core/ vim nginx.h cd /root/nginx- ...

  7. python3 + pycharm+requests+HTMLTestRunner生成不了测试报告html

    生成不了测试文件,是运行的方式不对.因为在运行的时候,pycharm默认使用unit-test运行,所以没有生成测试报告.至于为什么会这样子,我就不清楚了,不过想了解更多的朋友,可以百度一下. 解决的 ...

  8. JavaScript中使用ActiveXObject操作本地文件夹的方法

    转载地址    http://www.jb51.net/article/48538.htm 在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实 ...

  9. 《高性能mysql》笔记

    组合索引和sql中的顺序有关 单列索引和sql中的顺序无关

  10. log4j2重复打印日志问题解决

    log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <Configuration> &l ...