BZOJ 4326 树链剖分+二分+差分+记忆化
去年NOIP的时候我还不会树链剖分!
还是被UOJ 的数据卡了一组。
差分的思想还是很神啊!
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
const int Maxn=;
const int Inf=0x3f3f3f3f;
int n,m,u,v,w,cnt,tot,MaxLen,MinLen,Minw,Maxw,Ans;
int head[Maxn],dep[Maxn],siz[Maxn],father[Maxn],W[Maxn],p[Maxn],q[Maxn],dis[Maxn],Len[Maxn],c[Maxn],top[Maxn],Memory[Maxn],Root;
struct Edge{int to,next,w;}edge[Maxn<<];
inline void Get_Int(int &x)
{
x=; char ch=getchar(); int f=;
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();} x*=f;
}
inline void Put_Int(int x)
{
char ch[]; int top=;
if (x==) ch[++top]='';
while (x) ch[++top]=x%+'',x/=;
while (top) putchar(ch[top--]); putchar('\n');
}
inline int Max(int x,int y) {return x>y?x:y;}
inline int Min(int x,int y) {return x>y?y:x;}
inline void Add(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;}
//==============================================
void Dfs1(int u,int fa)
{
siz[u]=;
for (int i=head[u];i!=-;i=edge[i].next)
if (edge[i].to!=fa)
{
dep[edge[i].to]=dep[u]+;
father[edge[i].to]=u;
dis[edge[i].to]=dis[u]+edge[i].w;
W[edge[i].to]=edge[i].w;
Dfs1(edge[i].to,u);
siz[u]+=siz[edge[i].to];
}
}
void Dfs2(int u,int chain,int fa)
{
top[u]=chain; int k=;
for (int i=head[u];i!=-;i=edge[i].next)
if (edge[i].to!=fa && (siz[edge[i].to]>siz[k] || k==)) k=edge[i].to;
if (k==) return;
Dfs2(k,chain,u);
for (int i=head[u];i!=-;i=edge[i].next)
if (edge[i].to!=fa && k!=edge[i].to) Dfs2(edge[i].to,edge[i].to,u);
}
inline int Lca(int u,int v)
{
while (true)
{
if (top[u]==top[v]) return dep[u]>dep[v]?v:u;
if (dep[top[u]]>dep[top[v]]) u=father[top[u]]; else v=father[top[v]];
}
}
int Get(int u,int f)
{
for (int i=head[u];i!=-;i=edge[i].next)
if (f!=edge[i].to)
c[u]+=Get(edge[i].to,u);
if (c[u]==tot) MinLen=Max(MinLen,W[u]);
return c[u];
}
inline bool Check(int t)
{
MaxLen=MinLen=tot=;
memset(c,,sizeof(c));
for (int i=;i<=m;i++)
if (Len[i]>t)
{
c[p[i]]++,c[q[i]]++;
c[Lca(p[i],q[i])]-=;
MaxLen=Max(MaxLen,Len[i]);
tot++;
}
if (Memory[tot])
{
if (Memory[tot]<=t) return true;
return false;
}
Get(Root,Root);
Memory[tot]=(MaxLen-MinLen);
if (Memory[tot]<=t) return true;
return false;
}
int main()
{
srand(time());
Get_Int(n),Get_Int(m);
memset(head,-,sizeof(head)); Minw=Inf;
for (int i=;i<n;i++)
{
Get_Int(u),Get_Int(v),Get_Int(w);
Add(u,v,w),Add(v,u,w);
Minw=Min(Minw,w);
}
for (int i=;i<=m;i++) Get_Int(p[i]),Get_Int(q[i]);
Root=rand()%n+;
father[Root]=Root; dep[Root]=; dis[Root]=;
Dfs1(Root,Root);
Dfs2(Root,Root,Root);
memset(Memory,,sizeof(Memory));
for (int i=;i<=m;i++) Len[i]=dis[p[i]]+dis[q[i]]-*dis[Lca(p[i],q[i])],Maxw=Max(Maxw,Len[i]);
int l=Minw,r=Maxw;
while (l<=r)
{
int mid=(l+r)>>;
if (Check(mid)) Ans=mid,r=mid-; else l=mid+; }
Put_Int(Ans);
return ;
}
C++
BZOJ 4326 树链剖分+二分+差分+记忆化的更多相关文章
- hdu4729 树链剖分+二分
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
- NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)
Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...
- BZOJ_4326_[NOIP2015]_运输计划_(二分+LCA_树链剖分/Tarjan+差分)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4326 给出一棵带有边权的树,以及一系列任务,任务是从树上的u点走到v点,代价为u到v路径上的权 ...
- BZOJ 4551[Tjoi2016&Heoi2016]树(树链剖分+二分)
Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记 ...
- BZOJ 3631 松鼠的新家 - 树链剖分 / 树上差分
传送门 分析: 树链剖分:x->y,将x到y的路径加一,并将x端点的答案-1,最后统计答案. 树上差分:x->y,x+1,y+1,lca-1,fa[lca]-1,并将x打上标记,最后统计前 ...
- 【BZOJ3307】雨天的尾巴 题解(树链剖分+树上差分)
题目链接 题目大意:给定一颗含有$n$个结点的树,每次选择两个结点$x$和$y$,对从$x$到$y$的路径上发放一带$z$类型的物品.问完成所有操作后每个结点发放最多的时哪种物品. 普通的树链剖分貌似 ...
- BZOJ 1036 && 树链剖分
还是太弱啊..各种数据结构只听过名字却没有一点概念..树链剖分也在这个范畴..今天来进一步深化一下教育改革推进全民素质提高. 性质 忘了在哪里看到的一篇blog有一句话讲得非常好,树链剖分不是一种数据 ...
- bzoj 3083 树链剖分
首先我们先将树提出一个根变成有根树,那么我们可以通过树链剖分来实现对于子树的最小值求解,那么按照当前的根和询问的点的相对位置关系我们可以将询问变成某个子树和或者除去某颗子树之后其余的和,前者直接询问区 ...
- bzoj 2243 树链剖分
2013-11-19 16:21 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=2243 树链剖分,用线段树记录该区间的颜色段数,左右端点颜 ...
随机推荐
- 一. HTML认识
1.1认识什么是纯文本文件txt window自带有一个软件,叫做记事本,记事本保存的格式就是TXT,就是英文text的缩写,术语上称呼为"纯文本文件. TXT文件,只能保存文本内容,是无法 ...
- noi 8787 数的划分
题目链接:http://noi.openjudge.cn/ch0206/8787/ 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 简直跟分苹果一模一样. #include < ...
- (1) 第一章 Java体系结构介绍
1.网络带来的挑战和机遇 (1).挑战一: 网络包含的设备越来越广泛, 硬件体系不同, 操作系统不同,用途不同. java解决办法: 通过创建与平台无关的程序来解决这个问题.一个java程序可以不需要 ...
- 控制边框颜色:《CSS3 Border-color》
CSS3中有关于Border的属性一共有三个:圆角border-radius,图片边框border-images,边框多颜色border-color,其中圆角border-radius是常用的一个属性 ...
- 2016年12月17日 星期六 --出埃及记 Exodus 21:12
2016年12月17日 星期六 --出埃及记 Exodus 21:12 "Anyone who strikes a man and kills him shall surely be put ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
- Doolitter分解 三对角矩阵分解 拟三对角分解
#include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> # ...
- php中的访问修饰符 private、protected、public的作用范围
1. private 只能在类内部使用 2. protected 可以在类内部和继承类里使用.类外部不能使用[即实例化后的对象无法调用] 3. public 全部范围适用. 4.子类复写父类中的方法时 ...
- XAF应用开发教程-内置Attribute功能列表
在 XAF 框架,一些用来生成一个业务应用程序的信息是在Attribute中指定.您可以将属性应用到业务类 (或它的成员) 指定验证规则,指定如何对数据进行显示. 设置关系类等.本主题提供了有关在何处 ...
- javascript事件代理(Event Delegation)
看了几篇文章,放上来供参考 司徒正美的文章,Event Delegation Made Easy --------------------------------------------------- ...