题目描述

给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小。

数据范围:\(1\le K \le N \le 10^5\)

解题思路

这道题我有两种方法。

方法一

我们考虑一个性质:选出来的链一定会是直径的一部分

不然就肯定会存在可能更新最大值的一个分支,而且这个分支的大小一定会不比路径包含在直径上时小。

同样的道理,我们发现这条路径在直径上越长越好。

那么我们不妨先把直径抠出来,记作一个序列,那么这颗树就可以想象成一条链挂了很多子树。

我们dfs出每一个直径上的点,它下面的子树的以根为源点的最长路,然后用 \(\text{ST}\) 表维护一下它的区间最大值,再加上一点前缀和,然后在直径上移动这个序列,更新答案就好了。

复杂度是 \(O(n \log n)\) 的。

方法二

考虑一种贪心的思想。

我们考虑不断删掉叶子,每次选择距离目标形态最近的叶子删掉,并且判断一下他的父亲是不是成了新的叶子,并将他的答案向父亲合并。

关于正确性:

  1. 删成链之后就会继续删链的端点,这肯定没问题。
  2. 这里的最近类似与上文中的“直径上的点的子树的以根为源点的最长路”,所以是对的。
  3. 由于选择顺序的规定,所以更新父亲的一定是最后被选的一个儿子,因为它的关键字最大,所以只需要在父亲变叶子时更新。

知道了原理后,我们可以用 set 存边,方便进行删边操作,用 priority_queue 来维护所有的叶子,然后直接模拟这个过程就好了。

细节注意事项

  • 用第一种方法会稍微有一点难调。
  • 用第二种方法就可能会出现 STL 的使用出错等问题。

参考代码

这是方法一的代码,由于我自己没(lan)有(de)打,所以这里放的是 \(\text{\color{black}{M}\color{red}{\_sea}}\) 神仙的代码

// ===================================
// author: M_sea
// website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
using namespace std; inline int read() {
int X=0,w=1; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
return X*w;
} const int N=100000+10; int n,k; struct edge { int v,w,nxt; } e[N<<1];
int head[N];
inline void addEdge(int u,int v,int w) {
static int cnt=0;
e[++cnt]=(edge){v,w,head[u]},head[u]=cnt;
} int rt1,rt2;
int dis[N],fa[N];
inline void dfs1(int u,int f) {
if (dis[u]>dis[rt1]) rt1=u;
for (re int i=head[u];i;i=e[i].nxt)
if (e[i].v!=f) dis[e[i].v]=dis[u]+e[i].w,dfs1(e[i].v,u);
}
inline void dfs2(int u,int f) {
fa[u]=f;
if (dis[u]>dis[rt2]) rt2=u;
for (re int i=head[u];i;i=e[i].nxt)
if (e[i].v!=f) dis[e[i].v]=dis[u]+e[i].w,dfs2(e[i].v,u);
} int cnt=0,lend=0;
int in[N],maxdis[N],w[N];
int lg[N],st[17][N],sum[N];
inline void dfs3(int u,int f) {
for (re int i=head[u];i;i=e[i].nxt) {
int v=e[i].v; if (v==f) continue;
w[v]=e[i].w,dfs3(v,u);
if (!in[v]) maxdis[u]=max(maxdis[u],maxdis[v]+e[i].w);
}
}
inline void init_diameter() {
for (re int i=rt2;i;i=fa[i]) in[i]=1,++cnt;
dfs3(rt1,0);
for (re int i=2;i<=n;++i) lg[i]=lg[i>>1]+1;
for (re int i=rt2,j=1;i;i=fa[i],++j) st[0][j]=maxdis[i];
for (re int i=1;i<17;++i)
for (re int j=1;j+(1<<i)-1<=cnt;++j)
st[i][j]=max(st[i-1][j],st[i-1][j+(1<<(i-1))]);
for (re int i=rt2,j=1;i;i=fa[i],++j) sum[j]=w[i],lend+=w[i];
for (re int i=1;i<=cnt;++i) sum[i]+=sum[i-1];
}
inline int query(int l,int r) {
int t=lg[r-l+1];
return max(st[t][l],st[t][r-(1<<t)+1]);
} int main() {
n=read(),k=read();
if (n==1) { puts("0"); return 0; }
for (re int i=1;i<n;++i) {
int u=read(),v=read(),w=read();
addEdge(u,v,w),addEdge(v,u,w);
}
dfs1(1,0),dfs2(rt1,0);
init_diameter();
if (k>cnt) printf("%d\n",query(1,cnt));
else {
int ans=2e9;
for (re int i=1;i+k-1<=cnt;++i) {
int tmp=max(sum[i-1],lend-sum[i+k-2]);
ans=min(ans,max(tmp,query(i,i+k-1)));
}
printf("%d\n",ans);
}
return 0;
}

下面是方法二的代码,我自己敲的。

/*
================================
Author: The Ace Bee
Blog: www.cnblogs.com/zsbzsb
This code is made by The Ace Bee
================================
*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 100000 + 10; int n, k, ans;
struct node{ int u, w; };
inline bool operator < (const node& x, const node& y) { return x.w > y.w; }
priority_queue < node > Q;
set < pair < int, int > > s[_]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(k);
for (rg int u, v, d, i = 1; i < n; ++i) {
read(u), read(v), read(d);
s[u].insert(make_pair(v, d));
s[v].insert(make_pair(u, d));
}
for (rg int i = 1; i <= n; ++i)
if (s[i].size() == 1) Q.push((node) { i, (*s[i].begin()).second });
while (Q.size() > 2 || k < n) {
node x = Q.top(); Q.pop(), --n, ans = x.w;
int u = x.u, v = (*s[u].begin()).first;
s[v].erase(s[v].lower_bound(make_pair(u, 0)));
if (s[v].size() == 1) Q.push((node) { v, ans + (*s[v].begin()).second });
}
printf("%d\n", ans);
return 0;
}

完结撒花 \(qwq\)

「CF1004E」Sonya and Ice Cream的更多相关文章

  1. E. Sonya and Ice Cream(开拓思维)

    E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心

    题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...

  3. CodeForces - 1004E Sonya and Ice Cream

    题面在这里! 挺智障的一个二分...我还写了好久QWQ,退役算啦 题解见注释... /* 先对每个点记录 向子树外的最长路 和 向子树内最长路,然后二分. 二分的时候枚举链的LCA直接做就好啦. */ ...

  4. Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)

    网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自https://www.cnblogs.com/qldabiaoge/p/9315722.html. 首先用set数组维护每一个节点所连接的边的 ...

  5. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  6. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  7. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  8. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  9. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

随机推荐

  1. 你了解真正的 restful API 吗?

    本文原创地址,博客:https://jsbintask.cn/2019/03/20/api/restful-api-best-practices/(食用效果最佳),转载请注明出处! 前言 在以前,一个 ...

  2. ZooKeeper-集群模式配置

    (1)下载安装zookeeper,进行基本的配置,详细教程:https://www.cnblogs.com/excellencesy/p/11956485.html (2)在三台虚拟机上分别按照以上方 ...

  3. easyapi用法

    https://www.easyapi.com/api/?documentId=1773&themeId=

  4. mcast_get_ttl函数

    #include <errno.h> #include <net/if.h> #include <sys/socket.h> #include <netine ...

  5. 使用 yum 安装 MariaDB 与 MariaDB 的简单配置

    1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB,两条命令都可以 systemctl sta ...

  6. 5_6 团体队列(UVa540)<queue与STL其他容器的综合运用>

    先给出T个团体,并给出每个团体有多少人和每个人的编号,然后所有团体一起排队,排成一条大队列,排队的原则是,一个成员加入,如果这个成员所在的团体已经有人在排队了,那么他就加到他所在团体的最后面,而不是整 ...

  7. 4_6 师兄帮帮忙(UVa12412)(选做)

    你好,我是外国语学院的本科生.正如你所知道的,编程是一个  在我们大学必修课,即使他/她的专业是远离计算机科学.我根本不喜欢这个 课程,因为我不擅长电脑,我不想有任何编程的尝试!  但我不得不做作业: ...

  8. Ubuntu python3 与 python2 的 pip调用

    ubuntu 是默认装有pytthon2.x 与 python3.x 共存的 通常终端里 python 表示 python2 版本 python3 表示 python3 版本 (如果你没更改软链接设置 ...

  9. leetcode 0215

    目录 ✅ 1002. 查找常用字符 描述 解答 java other's implementation(todo watch me) cpp py ✅ 821. 字符的最短距离 描述 解答 cpp p ...

  10. FTP文件传输服务!

    一.FTP  连接及传输模式 1.控制连接:TCP 21,用于发送 FTP 命令信息2.数据连接:TCP 20,用于上传.下载数据3.数据连接的建立类型: (1)主动模式:服务器主动发起数据连接 (2 ...