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个距离),现在要 ...
随机推荐
- Win10每次开机总是自动弹出MSN网址导航如何取消
Win10每次开机总是自动弹出MSN网址导航如何取消 近来有用户在升级Win10系统后,每次开机总是会自动弹出MSN中文网的网址导航.如果不想要开机打开MSN网址导航,那么应该怎么设置来取消呢?对此, ...
- 对接极光IM之修改头像
因为项目中使用了极光IM,在对接极光的时候,发现了如果想要在改变自己个人中心的头像同时改变极光IM的头像,就必须要将本地磁盘的文件上传到极光服务器,根据反馈的media_id来进行修改头像. 但是因为 ...
- 10.8 wtx模拟题题解
填坑 orz w_x_c_q w_x_c_q的模拟赛(150pts,炸了) money 题目背景: 王小呆又陷入自己的梦里.(活在梦里...) 题目描述: 王小呆是一个有梦想的小菜鸡,那就是赚好多好多 ...
- 【洛谷2324】[SCOI2005]骑士精神 IDA*
[SCOI2005]骑士精神 描述 在一个\(5×5\)的棋盘上有\(12\)个白色的骑士和\(12\)个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑 士的走法(它可以走到和它横坐标相差为 ...
- POJ1057 FILE MAPPING
题目来源:http://poj.org/problem?id=1057 题目大意:计算机的用户通常希望能够看到计算机存储的文件的层次结构的图形化表示.Microsoft Windows的 " ...
- 树莓派使用 HLS 实现视频流直播
说明 这次介绍一下基于上一篇文章"树莓派编译安装 FFmpeg "的应用,即 HLS 视频流直播.原理是 FFmpeg 将 USB 摄像头的原始视频流压缩为 H.264 视频流,然 ...
- Mybatis学习笔记(四) —— SqlMapConfig.xml配置文件
一.properties(属性) SqlMapConfig.xml可以引用java属性文件中的配置信息 在config下定义db.properties文件,如下所示: db.properties配置文 ...
- P3172 [CQOI2015]选数(莫比乌斯反演)
[题目链接] https://www.luogu.org/problemnew/show/P3172 [题解] https://www.luogu.org/blog/user29936/solutio ...
- Quartz中Cron详解
Quartz中的cron跟Linux系统的cron定义不太一样(Linux从分开始) 特殊字符: * 用来表示包含一个范围内的任意值. 例如, 分钟位置的“*” 表示 “每分钟”. ? 当不特定指代 ...
- Query获取多种input值的方法
1 if($("input[name=item][value='val']").attr('checked')==true) //判断是否已经打勾 name即控件name属性,va ...