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个距离),现在要 ...
随机推荐
- 吴裕雄 python 机器学习——等度量映射Isomap降维模型
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...
- vue 路由里面的 hash 和 history
对于 Vue 这类渐进式前端开发框架,为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义.前端路由的核心,就在于 —— 改变视图的同时不会向后端发出请求. ...
- POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- cuda&vs2010的属性配置
平时总需要新建工程,但是却总忘记该修改哪里,于是寻找了官方的项目,截下其中的属性修改图. 1 2 3 4 5 6 7 8 9 10 11
- kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- php 多语言(UTF-8编码)导出Excel、CSV乱码解决办法之导出UTF-8编码的Excel、CSV
新项目,大概情况是这样的:可能存在多国.不同语种使用者,比喻有中文.繁体中文,韩文.日本等等,开发时选择了UTF-8编码,开发顺利,没有问题.昨天做了一个csv导出功能,导出的东西完全乱了: 设置mb ...
- element,点击查看,实现tab切换:
点击查看,实现tab切换: 代码如下: <template> <div> <el-table :data="tableData" style=&quo ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_IL和验证
1.IL 基于栈——所有指令压入一个执行栈,并从栈弹出结果. 2.IL 指令无类型——指令会判断栈中操作数的类型,并执行恰当的操作. 3.IL 最大优势——应用程序的健壮性和安全性. 将 IL 编译成 ...
- day23 模块
1. 模块 1. 首先,我们先看个老生常谈的问题. 什么是模块. 模块就是一个包含了python定义和声 明的文件, 文件名就是模块的名字加上.py后缀. 换句话说我们目前写的所有的py文件都可以 看 ...
- Android 选择本地图片的demo
https://github.com/lipanquan/SelectLocalPhoto