This article is made by Jason-Cow.
Welcome to reprint.
But please post the writer's address.

http://www.cnblogs.com/JasonCow/

[NOIP2015]运输计划    Hello!链剖。你好吗?

题意:

给出一棵n个节点的带权树,m对树上点对

现在允许删除一条边,(权值修改为0)

输出: 最小化的点对间最大距离

1.链剖

2.树上差分

3.二分

链剖我就不多说了,就是两dfs

注意:要在dfs1中多维护一个dis[x],x到root的距离,顺便记录一下w[x]!

 void dfs1(int u,int f){
fa[u]=f,dep[u]=dep[f]+,siz[u]=;
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v!=f){
w[v]=E[i].w,dis[v]=dis[u]+E[i].w;
dfs1(v,u);
siz[u]+=siz[v];
if(!son[u]||siz[v]>siz[son[u]])son[u]=v;
}
}
}

dfs1

 void dfs2(int u,int t){
dfn[u]=++idx,top[u]=t;
if(son[u])dfs2(son[u],t);
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v!=fa[u]&&v!=son[u])dfs2(v,v);
}
}

dfs2

树上差分,先差分,后dfs上放(95分的根本原因,不过好写)

细节:add(l,r)  <=>  cf[l]++ , cf[r+1]--; 再上放

 void modify(int u,int f){
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v!=f){
modify(v,u);
cf[u]+=cf[v];
}
}
}

modify

二分,很显然好吗。

 bool check(int x){
int cnt=,maxcost=;
memset(cf,,sizeof(cf));
for(int i=;i<=m;i++)
if(a[i].dis>x){
cnt++;
maxcost=max(maxcost,a[i].dis-x);
cf[a[i].s]++,cf[a[i].t]++;cf[a[i].lca]-=;
}
if(cnt==)return false;
modify(,);
for(int i=;i<=n;i++)
if(cf[i]==cnt && w[i]>=maxcost)
return false;
return true;
}

check

这个还不是正解,因为被卡常了,于是我就卡数据,嘿嘿嘿~~

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int N=3e5+,M=6e5+;
inline void _(int &ans){
ans=;char x=getchar(),f=;
while(x<''||x>''){if(x=='-')f=;x=getchar();}
while(x>=''&&x<='')ans=ans*+x-'',x=getchar();
if(f)ans=-ans;
} int head[N],tot,n,m;
int dfn[N],top[N],siz[N],son[N],dep[N],fa[N],idx;
int dis[N],w[N],cf[N];
struct node{int v,w,next;}E[M];
struct ask{int lca,dis,s,t;}a[N];
void add(int u,int v,int w){E[++tot]=(node){v,w,head[u]},head[u]=tot;}
void edge(int u,int v,int w){add(u,v,w),add(v,u,w);} void dfs1(int u,int f){
fa[u]=f,dep[u]=dep[f]+,siz[u]=;
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v!=f){
w[v]=E[i].w,dis[v]=dis[u]+E[i].w;
dfs1(v,u);
siz[u]+=siz[v];
if(!son[u]||siz[v]>siz[son[u]])son[u]=v;
}
}
} void dfs2(int u,int t){
dfn[u]=++idx,top[u]=t;
if(son[u])dfs2(son[u],t);
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v!=fa[u]&&v!=son[u])dfs2(v,v);
}
} void lca(int s,int t,int i){
int x=s,y=t;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
a[i].lca=x;
a[i].dis=dis[s]+dis[t]-*dis[x];
} void modify(int u,int f){
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v!=f){
modify(v,u);
cf[u]+=cf[v];
}
}
} bool check(int x){
int cnt=,maxcost=;
memset(cf,,sizeof(cf));
for(int i=;i<=m;i++)
if(a[i].dis>x){
cnt++;
maxcost=max(maxcost,a[i].dis-x);
cf[a[i].s]++,cf[a[i].t]++;cf[a[i].lca]-=;
}
if(cnt==)return false;
modify(,);
for(int i=;i<=n;i++)
if(cf[i]==cnt && w[i]>=maxcost)
return false;
return true;
} int _u,_v,_w,_s,_t;
int l,r,mid;
int main(){
_(n),_(m);//scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
_(_u),_(_v),_(_w);//scanf("%d%d%d",&_u,&_v,&_w);
edge(_u,_v,_w);
}
dfs1(,);
dfs2(,);
for(int i=;i<=m;i++){
_(_s),_(_t);//scanf("%d%d",&_s,&_t);
a[i].s=_s,a[i].t=_t;
lca(_s,_t,i);
r=max(r,a[i].dis);
}
l=max(,r-);//95分
while(l<=r){
int mid=(l+r)>>;
if(check(mid))l=mid+;
else r=mid-;
}
printf("%d\n",l);
return ;
}

树链剖分-Hello!链剖-[NOIP2015]运输计划-[填坑]的更多相关文章

  1. 数据结构(树链剖分):COGS 2109. [NOIP2015] 运输计划

    2109. [NOIP2015] 运输计划 ★★★   输入文件:transport.in   输出文件:transport.out   简单对比时间限制:1 s   内存限制:256 MB [题目描 ...

  2. [NOIP2015]运输计划 线段树or差分二分

    目录 [NOIP2015]运输计划 链接 思路1 暴力数据结构 思路2 二分树上差分 总的 代码1 代码2 [NOIP2015]运输计划 链接 luogu 好久没写博客了,水一篇波. 思路1 暴力数据 ...

  3. NOIP2015 运输计划(bzoj4326)

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 886  Solved: 574[Submit][Status] ...

  4. cogs2109 [NOIP2015] 运输计划

    cogs2109 [NOIP2015] 运输计划 二分答案+树上差分. STO链剖巨佬们我不会(太虚伪了吧 首先二分一个答案,下界为0,上界为max{路径长度}. 然后判断一个答案是否可行,这里用到树 ...

  5. bzoj 4326: NOIP2015 运输计划

    4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MB Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个 ...

  6. [BZOJ4326][codevs4632][codevs5440][UOJ#150][NOIP2015]运输计划

    [BZOJ4326][codevs4632][codevs5440][UOJ#150][NOIP2015]运输计划 试题描述 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− ...

  7. [NOIP2015]运输计划 D2 T3 LCA+二分答案+差分数组

    [NOIP2015]运输计划 D2 T3 Description 公元2044年,人类进入了宇宙纪元. L国有n个星球,还有n-1条双向航道,每条航道建立在两个星球之间,这n-1条航道连通了L国的所有 ...

  8. AC日记——[NOIP2015]运输计划 cogs 2109

    [NOIP2015] 运输计划 思路: 树剖+二分: 代码: #include <cstdio> #include <cstring> #include <iostrea ...

  9. 题解 [NOIP2015]运输计划

    题解 [NOIP2015]运输计划 题面 解析 首先肯定是要求出每条路径的长度. 这个用节点到根的前缀和就行了(一开始脑抽写了个线段树...) 然后有一个显然的类似贪心的想法, 就是你改造的边肯定在最 ...

随机推荐

  1. 【Python】表白程序

     程序链接:https://www.lanzous.com/i8xj5mh # 打包操作 # 安装pyinstaller # cmd输入 pip install pyinstaller # shift ...

  2. 使用VSCode创建简单的Razor Webapp--1.入门

    1.下载vscode,安装dotnet core sdk 在cmd中使用命令dotnet --version可以查看当前安装的版本 2.打开vscode,设置语言和扩展 在最左边的工具栏,点击最下面的 ...

  3. php Allowed memory size of 134217728 bytes exhausted

    报错:PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in ...

  4. webpack 之loader

      webpack的作用: 是 用来处理我们写的js代码.并且会自动处理js之间相关的依赖. 但是,开发中我们不仅仅有基本的js代码处理,还需要加载css,图片,也包括一些高级的 将ES6转成ES5代 ...

  5. mybatis一对多 多对一 多对多

    https://blog.csdn.net/AdminGuan/article/details/98952484   Mybatis的Mapper该如何编写多对一? 很简单,就是在resultMap标 ...

  6. 四、CentOS 7安装Oracle JDK

    CentOS 7安装Oracle JDK,查看Linux是否自带的JDK,如有openJDK,则卸载  CentOS7.1 JDK安装 1.卸载自带OPENJDK    用 java -version ...

  7. Codeforces Round #614 (Div. 2) C - NEKO's Maze Game

    题目链接:http://codeforces.com/contest/1293/problem/C 题目:给定一个 2*n的地图,初始地图没有岩浆,都可以走, 给定q个询问,每个询问给定一个点(x,y ...

  8. Consider using the `--user` option or check the permissions.

    安装pip install C:\Users\道路\Documents\EGDownloads\pip-1.0.tar.gz 报错:Consider using the `--user` option ...

  9. Docker(二)Image 与网络

    Docker Image 我们介绍一下如何构造一个自定义的 Docker  Image.在Docker 中,我们使用Dokcerfile 构建一个docker的描述. 首先我们定义一下需要启动一个什么 ...

  10. 在sql server中如何检测一个字符串中是否包含另一个字符串

    select CHARINDEX('456','123456')   SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字符串中的方法: 一.CHARINDEX函数介绍 1.函数功 ...