poj 3162 树DP+单调队列
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+单调队列的更多相关文章
- BZOJ1023:[SHOI2008]cactus仙人掌图(圆方树,DP,单调队列)
Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus). 所谓简单回路就是指在图上不重复经过任何一个顶点 ...
- 1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP
1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP 题意 用摄像机观察动物,有两个摄像机,一个可以放在奇数天,一个可以放在偶数天.摄像机在 ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- bzoj 1171 并查集优化顺序枚举 | 线段树套单调队列
详见vfleaking在discuss里的题解. 收获: 当我们要顺序枚举一个序列,并且跳过某些元素,那么我们可以用并查集将要跳过的元素合并到一起,这样当一长串元素需要跳过时,可以O(1)跳过. 暴力 ...
- (noip模拟二十一)【BZOJ2500】幸福的道路-树形DP+单调队列
Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的小T发现可以用树来描绘这个草图. ...
- DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)
codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Descripti ...
- 习题:烽火传递(DP+单调队列)
烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...
- 3622 假期(DP+单调队列优化)
3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...
- POJ 3162 Walking Race(树形dp+单调队列 or 线段树)
http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...
随机推荐
- CF581C Developing Skills 模拟
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main cha ...
- jvm与tomcat启动优化配置
JVM 优化 Java 的内存模型分为: Young,年轻代(易被 GC).Young 区被划分为三部分,Eden 区和两个大小严格相同的 Survivor 区,其中 Survivor 区间中,某一时 ...
- IDEA 中 使用 git(Git)
GitLab GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务.安装方法是参考GitLab在GitHub上的Wiki页面. Git Git( ...
- java 柱状图、折线图、饼状图
1.绘制柱状图: //BarChartTool工具类代码 package GUIview; import HibernateTool.HibernateTools; import ProductCla ...
- python 分页 封装
分页 封装 我是在项目根目录创建个分页文件 分页代码: class Pagination(object): def __init__(self, data_num, current_page, url ...
- java 学习原生jdbc
public class App { public static void main( String[] args ) { //JDBC驱动 String driverName = "com ...
- Go语言基础之9--指针类型详解
一. 变量和内存地址 每个变量都有内存地址,可以说通过变量来操作对应大小的内存 注意:通过&符号可以获取变量的内存地址 通过下面例子来理解下: 实例1-1 package main impor ...
- Go语言基础之8--面向对象编程1之结构体(struct)
一.结构体详解 1.1 声明和定义 1.Go中面向对象是通过struct来实现的, struct是用户自定义的类型 2.Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数 ...
- 关于在JS中设置标签属性
Attribute 该属性主要是用来在标签行内样式,添加.删除.获取属性.且适用于自定义属性. setAttribute("属性名",属性值“”):这个是用来设置标签属性的: re ...
- C语言指针收藏
指针是什么 >>每一个内存单元只能同时存储一个数据,如何保证内存单元同时只能存储一个数据,可以使用编号的方式实现内存单元标记,此编号就是指针. >>指针是一个变量,指针是存放着 ...