Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)
网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自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)的更多相关文章
- E. Sonya and Ice Cream(开拓思维)
E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 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 ...
- Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心
题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...
- CodeForces - 1004E Sonya and Ice Cream
题面在这里! 挺智障的一个二分...我还写了好久QWQ,退役算啦 题解见注释... /* 先对每个点记录 向子树外的最长路 和 向子树内最长路,然后二分. 二分的时候枚举链的LCA直接做就好啦. */ ...
- 「CF1004E」Sonya and Ice Cream
题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
随机推荐
- npm package管理
每个项目都包含一个package.json文件用来管理项目依赖的包以及项目相关信息. 其中比较总要的就是dependencies和devDependencies两项,分别指定了项目运行所依赖的模块.项 ...
- 51nod 1099 贪心/思维
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099 1099 任务执行顺序 基准时间限制:1 秒 空间限制:13107 ...
- android自定义日期组件之双DatePicker
1.效果图(两个DatePicker放在一起,同时选择起始与结束时间): 2.实现 2.1布局文件: <?xml version="1.0" encoding="u ...
- jspm
1.简介 JavaScript 模块的写法有几种,比如 AMD,CommonJS .. 还有标准化的 ES6 的写法 .. jspm 支持加载所有的用这些方法写的 JavaScript 模块 在你的应 ...
- 利用Pelican搭建个人博客
博客基于win7系统,python2.7和pelican. 1.安装工具 安装virtualenv pip install virtualenv 下载make,或者make移动至任一目录,并将路径写入 ...
- C#中的BackgroundWorker控件
C#中的BackgroundWorker控件 Keywords: C# .NET BackgroundWorkerSource: http://txw1958.cnblogs.com/ Backg ...
- http请求 详解
- vim-addon-manager install youcompleteme
/****************************************************************************** * vim-addon-manager ...
- UVA - 242 Stamps and Envelope Size (完全背包+bitset)
题意:给你一些邮票面值的集合,让你选择其中一个集合,使得“能用不超过n枚集合中的邮票凑成的面值集合S中从1开始的最大连续面值”(即mex(S)-1)最大.如果有多解,输出集合大小最小的一个:如果仍有多 ...
- tests
test