网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自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. mindmanager思维导图软件

    Mindjet 由原名 Mindjet MindManager 简化而来,是倍受赞誉.最好的思维导图软件.所谓思维导图实际上就是一种将你的思想具体化,把你的思维分析整理为有计划有条理的导向图的工作管理 ...

  2. mysql分组取topn

    本文来自  http://www.jb51.net/article/31590.htm 有些语句sql top n 是sqlserver语法 --按某一字段分组取最大(小)值所在行的数据 代码如下: ...

  3. shell for的用法

    #!/bin/sh for1(){ for i in 1 2 3 4 5 6do echo "$i"done } for1#!/bin/shfor2(){for i in {1.. ...

  4. hadoop-sqoop学习笔记

    ======导入==== sqoop import --connect jdbc:mysql://20.12.20.165:3306/luo0907 --username root --passwor ...

  5. Unity项目UI图片压缩格式(UGUI)

    http://blog.csdn.net/bobodan123/article/details/70316538 UI制作时候使用的是Ps 8位 RGB通道的色彩. 但导出的是16位RGBA色彩的图片 ...

  6. QString字符串拼接【转载】

    原文网址:http://www.cnblogs.com/ftrako/p/3481571.html for(intloop=1;loop<2;loop++) { QStringtest1=QSt ...

  7. python爬虫彩票案例,并自动发微信

    import requests from bs4 import BeautifulSoup import itchat import time,datetime all = [{1, 2, 3, 7, ...

  8. git revert reset

    git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit. git reset 是把HEAD向后移动了一下,而git revert是HEAD ...

  9. Angular5学习笔记 - 路由管理(五)

    一.添加路由管理引用 打开src/app/app.module.ts文件 import {RouterModule} from '@angular/router'; import {Routes} f ...

  10. -3dB的理解

    -3dB到底是什么?集成运放-3dB带宽又是什么? 以无源高通电路为例,介绍-3dB的意义. 输出与输入只比: Au=Uo/Ui=R/(R+1/j*2*PI*f*C)=1/(1+1/j*2*PI*f* ...