网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自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. 51nod 1131 数列

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1131 1131 覆盖数字的数量 基准时间限制:1 秒 空间限制:1310 ...

  2. linux下安装Java se和Eclipse

    首先要去下载好JDK,Java SE 8的官方网址是http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...

  3. filter()和sort()这两个方法一块学习,案例中。

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. _Meta 部分用法

    model.UserInfo._meta.app_label #获取该类所在app的app名称 model.UserInfo._meta.model_name #获取该类对应表名(字符串类型) mod ...

  5. Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

    数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...

  6. Linux C 编程内存泄露检测工具(一):mtrace

    前言 所有使用动态内存分配(dynamic memory allocation)的程序都有机会遇上内存泄露(memory leakage)问题,在Linux里有三种常用工具来检测内存泄露的情況,包括: ...

  7. ecmall中的分页问题

    <ecmall>Ecmall系统自带的分页功能 在Ecmall的二次开发中,分页是必不可少的.这个系统已经自带了分页功能,下面来看看如何使用这个分页. 下面是一个自定义的类,用于查看订单的 ...

  8. CERC2016 爵士之旅 Jazz Journey

    传送门(洛谷) 题目大意 给定$n$个位置,和一个长为$m$的序列$A$,你需要经过一条直接的边从第$A_i$个位置到第$A_{i+1}$个位置. 每条有向边$(u,v)$分为两种,第一种可以花费$C ...

  9. HAWQ 操作笔记

    1.HAWQ 是不支持主键和外建的,官方文档明确给出 Notes Using OIDs in new applications is not recommended. Avoid assuming t ...

  10. Android Studio导入项目,报错 Error:Unsupported method: BaseConfig.getApplicationIdSuffix().

    从GitHub上clone下来的第三方库,由于时间间隔很长,gradle的版本和本机的版本不一致,导入到Android Studio中会报错,错误信息如下: Error:Unsupported met ...