网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自https://www.cnblogs.com/qldabiaoge/p/9315722.html。

首先用set数组维护每一个节点所连接的边的信息,然后遍历一遍所有的点,把度为1的点放入集合s,(把距离作为第一要素);

然后把集合s中的点从小到大枚举,每个点存储的信息是该点及其该点的子节点中到该点的父亲节点的最大距离;

枚举一个点后,把它从集合s和它父亲节点中删除。如果这个时候父节点的度变成1了,说明这个节点是父亲节点到所有子节点中距离的最大值(比这个距离小的肯定比它先删除)。

然后就可以更新父亲节点及其所有子节点到父亲节点的父亲节点的最长距离。

循环的终止条件是n<=k且size<=2;首先要形成k个连续点,相当于要k-1条边,也就是要至少更新(n-1)-(k-1)=n-k次。

除此之外,这k个点肯定在直径上,因为我们要让离这k个点的最大距离尽量的小,而我们拓展点的时候,直径上的点距离拓展点最远,所以向直径方向拓展。

那么这k个点形成的答案树只有2个叶子节点,及集合s中只能最多剩余2个叶子节点到其它子节点的最大距离。

我们在枚举距离的时候是从小到大枚举,所以当满足终止条件的时候就是最小的最大距离。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
set<pair<int, int> > d[maxn], s;
int n, k, ans = 0;
int main() {
scanf("%d%d", &n, &k);
for (int i = 0 ; i < n - 1 ; i++ ) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
d[u].insert(make_pair(v, w));
d[v].insert(make_pair(u, w));
}
for (int i = 1 ; i <= n ; i++)
if (d[i].size() == 1) s.insert(make_pair((*d[i].begin()).second, i));
while( n > k || s.size() > 2) {
ans = (*s.begin()).first;
int i = (*s.begin()).second;
s.erase(s.begin());
int next = (*d[i].begin()).first;
d[next].erase(d[next].lower_bound(make_pair(i, 0)));
n--;
if (d[next].size() == 1)
s.insert(make_pair((*d[next].begin()).second + ans, next));
}
printf("%d\n", ans);
return 0;
}

  

Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)的更多相关文章

  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. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

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

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

  4. CodeForces - 1004E Sonya and Ice Cream

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

  5. 「CF1004E」Sonya and Ice Cream

    题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...

  6. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  7. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  8. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  9. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

随机推荐

  1. window上脚本到linux上不能用

    window上的脚本一定要用dos2unix 文件名处理一下

  2. Github-jcjohnson/torch-rnn代码详解

    Github-jcjohnson/torch-rnn代码详解 zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouwan  2016-3- ...

  3. android根据原图片的路径得到该图片的缩略图

    /** * 根据图片的路径得到该图片在表中的ID * @param cr * @param fileName * @return */ public static String getImageIdF ...

  4. centos下安装storm

    centOS安装ZeroMQ centOS安装ZeroMQ所需组件及工具: yum install gcc yum install gcc-c++ yum install make yum insta ...

  5. @angular/cli项目构建--animations

    使用方法一(文件形式定义): animations.ts import { animate, AnimationEntryMetadata, state, style, transition, tri ...

  6. stl_hash_set.h

    stl_hash_set.h // Filename: stl_hash_set.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  7. 排序算法 java实现2

    继续排序算法 4.冒泡排序 从第一个开始,跟后面一个数比较,如果比后面大就交换位置,这样没完成一轮,就可以把最大的选出来 public static <T extends Comparable& ...

  8. [Luogu4177][CEOI2008]order

    luogu sol 这题有点像网络流24题里面的太空飞行计划啊. 最大收益=总收益-最小损失. 先令\(ans=\sum\)任务收益. 源点向每个任务连容量为收益的边. 每个机器向汇点连容量为购买费用 ...

  9. mongodb所在目录空间不足解决方法

    1.原理是将目录/home/aa软连接到/usr/lib/下,以后从/usr/lib下读取的内容其实都是放在/home/aa下. 建议不要大范围动/usr下的内容,咋着也是属于系统目录,可能会对已装软 ...

  10. gulp之文件合并以及整合html中的script和link

    gulp的文件合并,也就是将多个js或css文件合并为一个的插件是:gulp-concat gulp将html中的多个<script>或<link>合并为一个的插件是:gulp ...