去年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 树链剖分+二分+差分+记忆化的更多相关文章

  1. hdu4729 树链剖分+二分

    An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (J ...

  2. NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

    Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...

  3. BZOJ_4326_[NOIP2015]_运输计划_(二分+LCA_树链剖分/Tarjan+差分)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=4326 给出一棵带有边权的树,以及一系列任务,任务是从树上的u点走到v点,代价为u到v路径上的权 ...

  4. BZOJ 4551[Tjoi2016&Heoi2016]树(树链剖分+二分)

    Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记 ...

  5. BZOJ 3631 松鼠的新家 - 树链剖分 / 树上差分

    传送门 分析: 树链剖分:x->y,将x到y的路径加一,并将x端点的答案-1,最后统计答案. 树上差分:x->y,x+1,y+1,lca-1,fa[lca]-1,并将x打上标记,最后统计前 ...

  6. 【BZOJ3307】雨天的尾巴 题解(树链剖分+树上差分)

    题目链接 题目大意:给定一颗含有$n$个结点的树,每次选择两个结点$x$和$y$,对从$x$到$y$的路径上发放一带$z$类型的物品.问完成所有操作后每个结点发放最多的时哪种物品. 普通的树链剖分貌似 ...

  7. BZOJ 1036 && 树链剖分

    还是太弱啊..各种数据结构只听过名字却没有一点概念..树链剖分也在这个范畴..今天来进一步深化一下教育改革推进全民素质提高. 性质 忘了在哪里看到的一篇blog有一句话讲得非常好,树链剖分不是一种数据 ...

  8. bzoj 3083 树链剖分

    首先我们先将树提出一个根变成有根树,那么我们可以通过树链剖分来实现对于子树的最小值求解,那么按照当前的根和询问的点的相对位置关系我们可以将询问变成某个子树和或者除去某颗子树之后其余的和,前者直接询问区 ...

  9. bzoj 2243 树链剖分

    2013-11-19 16:21 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=2243 树链剖分,用线段树记录该区间的颜色段数,左右端点颜 ...

随机推荐

  1. 读取NfcA格式数据

    如何读取数据? Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NfcA nfcA = NfcA.get(ta ...

  2. 【转】【C/C++】实现memcpy函数

    本文转自:http://my.oschina.net/renhc/blog/36345 面试中如问到memcpy的实现,那就要小心了,这里有陷阱. 先看下标准memcpy()的解释: ? 1 2 vo ...

  3. HTML 事件属性

    HTML 事件属性一:定义二:窗口事件 (Window Events)三:表单元素事件 (Form Element Events)四:图像事件 (Image Events) 一:定义 HTML 4 的 ...

  4. C++库汇总

    C++库汇总 C++类库介绍再次体现了C++保持核心语言的效率同时大力发展应用库的发展趋势!!在C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功 ...

  5. Query Designer中的特征限制(Characteristic Restrictions)、缺省值(Default Values)、自由特性(Free Characteristics)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. C library function - rewind()

    Description The C library function void rewind(FILE *stream) sets the file position to the beginning ...

  7. docker-registry使用笔记

    在国内docker-hub是肯定用不了的,不解释:sweat:. 所以最好还是建一个私有的docker-registry,存储一些常用的images方便随时pull. 相关链接 github:http ...

  8. (转载)5分钟安装Linux系统到U盘

    一.工具 使用 LinuxLive USB Creator 下载地址:http://xz2.cr173.com//soft/LinuxLiveusb.zip 二.操作步骤 1.下载linux系统镜像, ...

  9. Android属性动画完全解析(上),初识属性动画的基本用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系 ...

  10. laravel 中 与前端的一些事2 之使用Gulp编译sass

    下载所有依赖npm的packagist: 下载了前端laravel  elixir编译所需要的全部工具: gulp编译scss文件: scss文件的默认存放位置: 输入命令gulp 开始编译scss文 ...