bzoj 2500 幸福的道路 树上直径+set
首先明确:树上任意一点的最长路径一定是直径的某一端点。
所以先找出直径,求出最长路径,然后再求波动值<=m的最长区间
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<set>
#define N 1000005
using namespace std; int fa[N],cal[N],dis[2][N],d[N]; int e=1,head[N]; struct edge{
int u,v,w,next;
}ed[N]; void add(int u,int v,int w){
ed[e].u=u; ed[e].v=v; ed[e].w=w;
ed[e].next=head[u]; head[u]=e++;
} int L,R,it,maxn; bool bo[N]; void dfs(int x,int len){
bo[x]=1;
if(len>maxn) it=x,maxn=len;
if(!bo[fa[x]]) dfs(fa[x],len+cal[x]);
for(int i=head[x];i;i=ed[i].next){
if(!bo[ed[i].v])
dfs(ed[i].v,len+ed[i].w);
}
} int q[N],h,t; void bfs(int x,int wh){
bo[x]=1;int now,v,w; dis[wh][x]=0;
q[1]=x; h=t=1;
while(h<=t){
now=q[h++];
if(fa[now]&&!bo[fa[now]]&&dis[wh][fa[now]]<dis[wh][now]+cal[now]){
dis[wh][fa[now]]=dis[wh][now]+cal[now];
bo[fa[now]]=1; q[++t]=fa[now];
}
for(int i=head[now];i;i=ed[i].next){
v=ed[i].v; w=ed[i].w;
if(!bo[v]&&dis[wh][v]<dis[wh][now]+w){
dis[wh][v]=dis[wh][now]+w;
bo[v]=1; q[++t]=v;
}
}
}
} int n,m; int main()
{
//freopen("race.in","r",stdin);
//freopen("race.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=2;i<=n;i++){
scanf("%d%d",&fa[i],&cal[i]);
add(fa[i],i,cal[i]);
}
maxn=0; memset(bo,0,sizeof bo); dfs(1,0); L=it;
maxn=0; memset(bo,0,sizeof bo); dfs(L,0); R=it;
memset(bo,0,sizeof bo); bfs(L,0);
memset(bo,0,sizeof bo); bfs(R,1);
for(int i=1;i<=n;i++) d[i]=max(dis[0][i],dis[1][i]); //printf("%0.2lf\n",(double)clock()/CLOCKS_PER_SEC); multiset<int > ss;
int dd,xx,ll=1,ans=0;
for(int i=1;i<=n;i++){
ss.insert(d[i]);
dd=*(--ss.end());
xx=*(ss.begin());
while(dd-xx>m)
ss.erase(ss.find(d[ll++])),dd=*(--ss.end()),xx=*(ss.begin());
ans=max(ans,i-ll+1);
} printf("%d\n",ans);
return 0;
}
然而我打的依旧很蠢。。QAQ
bzoj 2500 幸福的道路 树上直径+set的更多相关文章
- [BZOJ 2500] 幸福的道路
照例先贴题面(汪汪汪) 2500: 幸福的道路 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 368 Solved: 145[Submit][Sta ...
- BZOJ 2500 幸福的道路(race) 树上直径+平衡树
structHeal { priority_queue<int> real; priority_queue<int> stack; void push(int x){ real ...
- ●BZOJ 2500 幸福的道路
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2500 题解: DFS,单调队列 首先有一个结论,距离树上某一个点最远的点一定是树的直径的一个 ...
- [BZOJ 2500]幸福的道路 树形dp+单调队列+二分答案
考试的时候打了个树链剖分,而且还审错题了,以为是每天找所有点的最长路,原来是每天起点的树上最长路径再搞事情.. 先用dfs处理出来每个节点以他为根的子树的最长链和次长链.(后面会用到) 然后用类似dp ...
- 【BZOJ】【2500】幸福的道路
树形DP+单调队列优化DP 好题(也是神题……玛雅我实在是太弱了TAT,真是一个250) 完全是抄的zyf的……orz我还是退OI保平安吧 第一步对于每一天求出一个从第 i 个点出发走出去的最长链的长 ...
- [Bzoj2500]幸福的道路(树上最远点)
2500: 幸福的道路 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 474 Solved: 194[Submit][Status][Discuss ...
- BZOJ2500: 幸福的道路
题解: 一道不错的题目. 树DP可以求出从每个点出发的最长链,复杂度O(n) 然后就变成找一个数列里最长的连续区间使得最大值-最小值<=m了. 成了这题:http://www.cnblogs.c ...
- bzoj2500幸福的道路 树形dp+单调队列
2500: 幸福的道路 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 434 Solved: 170[Submit][Status][Discuss ...
- BZOJ 4753 [Jsoi2016]最佳团体 | 树上背包 分数规划
BZOJ 4753 [Jsoi2016]最佳团体 | 树上背包 分数规划 又是一道卡精度卡得我头皮发麻的题-- 题面(--蜜汁改编版) YL大哥是24OI的大哥,有一天,他想要从\(N\)个候选人中选 ...
随机推荐
- MySQL 表名区分大小写设置
1.关闭MySQL服务: 控制面板主页-管理工具-服务-MySQL服务 2.在服务器运行目录找到my.ini 或者my.cnf文件: 在[mysqld]下面增加一行添加 :lower_ ...
- css选择器应用
.mynav li:not(:last-child) { margin-right: 20px; }
- Python 3 中的json模块使用
1. 概述 JSON (JavaScript Object Notation)是一种使用广泛的轻量数据格式. Python标准库中的json模块提供了JSON数据的处理功能. Python中一种非常常 ...
- IOS Swift语言开发 tableView的重用以及自cell的自适应高度
http://www.aichengxu.com/iOS/11143168.htm 一.准备数据 (这是一个元组,第一个元素为英雄的名字;第二个元素为英雄头像图片的名字,格式为.PNG,如果为其他的格 ...
- VirtualBox上安装64位系统
http://blog.csdn.net/mal327/article/details/6597263 原来VirtualBox安装64位的系统需要满足以下条件: 1.64位的cpu2.安装的系统必须 ...
- sql server 多行数据合并成一列
首先是源数据: ),cip.CheckIn_StartTime, )),cip.CheckIn_EndTime, )),cip.Rental_Price)) as content from Check ...
- ArrayList源码分析超详细
ArrayList源码分析超详解 想要分析下源码是件好事,但是如何去进行分析呢?以我的例子来说,我进行源码分析的过程如下几步: 找到类:利用 IDEA 找到所需要分析的类(ztrl+N查找ArraLi ...
- SpringMVC SessionAttributes 简述
使用SpringMVC时,我们会发现网络上有关SessionAttributes注解的内容非常少,更多的人甚至推荐你继续用HttpServletRequest中的session管理方法来控制Sessi ...
- 关于overflow的问题
<head> <title></title> <style type="text/css"> body { margin: 0; p ...
- DOM4J熟知
什么是解析xml 系统最终会从xml中读取数据. 读取的过程就是解析. CRUD ==> 增删改查 ==> create read update delete ==> 解析指的就是读 ...