题目链接:https://vjudge.net/problem/POJ-3162

题意:给一棵树,求每个结点的树上最远距离,记为a[i],然后求最大区间[l,r]满足区间内的max(a[i])-min(a[i])<=M。

思路:第一步向hdoj2196那题一样树形dp求出每个结点的最长距离,我的另一篇博客中有写到https://www.cnblogs.com/FrankChen831X/p/11375572.html。求出最远距离a[i]后,建立线段树维护区间的最大最小值。然后用两个指针i,j遍历一遍,每次求出[i,j]的最大最小值ans1和ans2,更新答案,因为j每次不用初始化,总复杂度为O(nlogn)。

AC代码:

#include<cstdio>
#include<algorithm>
using namespace std; typedef long long LL;
const int maxn=1e6+;
const LL inf=0x3f3f3f3f3f3f3f3f;
int n,ans,cnt,head[maxn],pt[maxn],a[maxn];
LL M,dp[maxn][],ans1,ans2; struct node1{
int v,nex;
LL w;
}edge[maxn<<]; struct node2{
int l,r;
LL Max,Min;
}tr[maxn<<]; void adde(int u,int v,LL w){
edge[++cnt].v=v;
edge[cnt].w=w;
edge[cnt].nex=head[u];
head[u]=cnt;
} void dfs1(int u,int fa){
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
LL w=edge[i].w;
if(v==fa) continue;
dfs1(v,u);
if(w+dp[v][]>dp[u][]){
dp[u][]=dp[u][];
dp[u][]=w+dp[v][];
pt[u]=v;
}
else if(w+dp[v][]>dp[u][])
dp[u][]=w+dp[v][];
}
} void dfs2(int u,int fa){
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
LL w=edge[i].w;
if(v==fa) continue;
if(v!=pt[u])
dp[v][]=w+max(dp[u][],dp[u][]);
else
dp[v][]=w+max(dp[u][],dp[u][]);
dfs2(v,u);
}
} void pushup(int v){
tr[v].Max=max(tr[v<<].Max,tr[v<<|].Max);
tr[v].Min=min(tr[v<<].Min,tr[v<<|].Min);
} void build(int v,int l,int r){
tr[v].l=l,tr[v].r=r;
if(l==r){
tr[v].Max=tr[v].Min=a[l];
return;
}
int mid=(l+r)>>;
build(v<<,l,mid);
build(v<<|,mid+,r);
pushup(v);
} void query(int v,int l,int r){
if(l<=tr[v].l&&r>=tr[v].r){
ans1=max(ans1,tr[v].Max);
ans2=min(ans2,tr[v].Min);
return;
}
int mid=(tr[v].l+tr[v].r)>>;
if(l<=mid) query(v<<,l,r);
if(r>mid) query(v<<|,l,r);
} int main(){
scanf("%d%lld",&n,&M);
for(int i=;i<=n;++i){
int v;LL w;
scanf("%d%lld",&v,&w);
adde(i,v,w);
adde(v,i,w);
}
dfs1(,);
dfs2(,);
for(int i=;i<=n;++i)
a[i]=max(dp[i][],dp[i][]);
build(,,n);
int j=;
for(int i=;i<=n;++i){
while(j<=n){
ans1=,ans2=inf;
query(,i,j);
if(ans1-ans2>M) break;
++j;
}
ans=max(ans,j-i);
}
printf("%d\n",ans);
return ;
}

poj3162(树形dp+线段树求最大最小值)的更多相关文章

  1. POJ 3162 Walking Race 树形DP+线段树

    给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...

  2. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

  3. Codeforces 671D. Roads in Yusland(树形DP+线段树)

    调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...

  4. 【洛谷5298】[PKUWC2018] Minimax(树形DP+线段树合并)

    点此看题面 大致题意: 有一棵树,给出每个叶节点的点权(互不相同),非叶节点\(x\)至多有两个子节点,且其点权有\(p_x\)的概率是子节点点权较大值,有\(1-p_x\)的概率是子节点点权较小值. ...

  5. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  6. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  7. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  8. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  9. CS Academy Gcd on a Circle(dp + 线段树)

    题意 给你一个长为 \(n\) 的环,你可以把它断成任意 \(k\) 段 \((1 < k \le n)\) ,使得每一段的 \(\gcd\) 都 \(>1\) . 问总共有多少种方案,对 ...

随机推荐

  1. MFC 下拉框Combo Box

    下拉框常用的事件是Change事件.属性常用:Data(英文;分隔),Sort(是否排序) // OnInitDialog()中 m_cbx.SetCurSel();//设置默认选项 //OnBnCl ...

  2. hdu 5572 An Easy Physics Problem 圆+直线

    An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. Splay - restudy

    https://www.zybuluo.com/wsndy-xx/note/1136246 图1 图2

  4. Towers of Hanoi Strike Back (URAL 2029)

    Problem The Tower of Hanoi puzzle was invented by French mathematician Édouard Lucas in the second h ...

  5. Win10+ Clion + 树莓派 + QT进行远程qt程序开发

    环境配置 环境配置 Windows上:QT5 + CLion 硬件上:一只可联网.可ssh连接且装有QT5的树莓派 暂时还没想好... 树莓派安装qt sudo apt-get update sudo ...

  6. AcWing:240. 食物链(扩展域并查集 or 带边权并查集)

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形. A吃B, B吃C,C吃A. 现有N个动物,以1-N编号. 每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用 ...

  7. @Value和@PropertySource实现*.properties配置文件读取过程和实现原理

    @Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1       配置使用步骤 (1)右击resource目录添加*.prooerties配置文件

  8. 【Elasticsearch】Docker 安装 Elasticsearch 2.4.4 版本(高版本方式不同)

    1. 下载  elasticsearch docker pull docker.elastic.co/elasticsearch/elasticsearch:6.4.3 2.启动 elasticsea ...

  9. 更新ubuntu的对应源配置文件

    UBUNTU中安装依赖包,出现如下错误:E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/o/openjdk-8/o ...

  10. 最大生成树+map实现技巧

    POJ2263 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include< ...