Code:

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std; #define maxn 50008
#define MAXR 500000001
#define ll int multiset <ll> S[maxn];
multiset <ll> :: iterator it;
int head[maxn], to[maxn << 1], nex[maxn << 1], val[maxn << 1];
int cnt,n, m, edges;
ll mid; void setIO(string a){
freopen((a+".in").c_str(),"r",stdin);
} void addedge(int u,int v,int c){
nex[++edges] = head[u], head[u] = edges, to[edges] = v, val[edges] = c;
} void read(){
scanf("%d%d",&n,&m);
for(int i = 1;i < n;++i){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
} ll dfs(int u,int fa){
S[u].clear();
for(int v = head[u]; v ; v = nex[v]){
if(to[v] == fa) continue;
ll t = dfs(to[v], u) + val[v];
if(t >= mid) ++cnt;
else S[u].insert(t);
} ll MAX = 0; while(!S[u].empty()){
if(S[u].size() == 1) return max(MAX, *S[u].begin());
it = S[u].lower_bound(mid - *S[u].begin());
if(it == S[u].begin() && S[u].count(*it) == 1) it++;
if(it == S[u].end()){
MAX = max(MAX, *S[u].begin());
S[u].erase(S[u].find(*S[u].begin()));
} else {
++cnt ;
S[u].erase(S[u].find(*it));
S[u].erase(S[u].find(*S[u].begin()));
}
} return MAX;
} bool check(){
cnt = 0;
dfs(1, 0);
if(cnt >= m) return true;
return false;
} void solve(){
ll l = 0, r = MAXR, ans = 0;
while(l <= r){
mid = (l + r) >> 1;
if(check()) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%lld", ans);
} void shut(){
fclose(stdin);
} int main(){
//setIO("input");
read();
solve();
shut();
return 0;
}

  

noip 2018 day1 T3 赛道修建 贪心_树上问题_multiset的更多相关文章

  1. noip 2018 day1 T1 铺设道路 贪心

    Code: #include<cstdio> using namespace std; int main() { int last=0,ans=0; int n;scanf("% ...

  2. 二分答案 + multiset || NOIP 2018 D1 T3 || Luogu P5021 赛道修建

    题面:P5021 赛道修建 题解:二分答案,用Dfs进行判断,multiset维护. Dfs(x,fa,Lim)用来计算以x为根的子树中有多少符合条件的路径,并返回剩余未使用的最长路径长. 贪心思想很 ...

  3. NOIP提高组2018 D1T3 【赛道修建】

    颓了好几天,终于把这到题处理了一下. 话说,其实我考场上想出正解了,但是手残,算复杂度的时候多按了一个零,导致算出来是1亿多的复杂度,都不敢打...就把部分分都捡了一下... 题目描述: C 城将要举 ...

  4. P5021 赛道修建[贪心+二分]

    题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,-,n1,2,-,n,有 n-1n−1 条适合于修建赛道的双向通 ...

  5. NOIP 2018 day1 题解

    今年noip的题和去年绝对是比较坑的题了,但是打好的话就算是普通水准也能350分以上吧. t1: 很显然这是一个简单的dp即可. #include<iostream> #include&l ...

  6. [NOIP 2018 Day1] 简要题解

    [题目链接] 铺设道路 : https://www.luogu.org/problemnew/show/P5019 货币系统 : https://www.luogu.org/problemnew/sh ...

  7. NOIP 2018 Day1

    Fei2Xue@Lian$Tian! 三道原题qwq真的凉 半年前看到有人发说说,梦见省选打开题目,是Please contact lydsy2012@163.com! 没想到一语成谶 大众分300 ...

  8. 洛谷P2668 斗地主==codevs 4610 斗地主[NOIP 2015 day1 T3]

    P2668 斗地主 326通过 2.6K提交 题目提供者洛谷OnlineJudge 标签搜索/枚举NOIp提高组2015 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 出现未知错误是说梗啊 ...

  9. NOIP 2014 Day1 T3飞扬的小鸟

    题目描述:http://codevs.cn/problem/3729/ 表示各种shabi的我编和调了半天,思路大体就是一个完全背包的模型,不过会多一些额外的转移(因为题目限制高度之类的),不过值得注 ...

随机推荐

  1. JS 去除字符串中的最后一个字符

    var str = 'Hello World!'; str = str.substr(0,str.length-1); alert(str);

  2. MFC中CFileDialog使用方法

    用CFileDialog选择了一个文件后,使用FILE::fopen打开文件错误,使用 的是相对地址.和王工调试了半天,怎么跟踪也没发现错误,原来如此. .... .. . . CFileDialog ...

  3. bzoj3438: 小M的作物(那年花开最小割)

    3438: 小M的作物 题目:传送门 题解: 最小割标准水题(做了几天的最小割之后表示是真的水) 为什么水:博主已经做过两道基本一样的题目了... 详情参考:bzoj3894 代码: #include ...

  4. action support分析

    Action这一部分主要是数据(索引)的操作和部分集群信息操作. 所有的请求通过client转发到对应的action上然后再由对应的TransportAction来执行相关请求.如果请求能在本机上执行 ...

  5. Pickling

    Pickle translates almost any type of object into a string. pickle.dumps takes an object as a paramet ...

  6. Lamp安装 php-v5.6【ZendGuardLoader】的问题

    Lamp安装 php-v5.6[ZendGuardLoader]的问题 标签(空格分隔):php,linux Apache日志: 就这个问题导致无法解析运行php文件.下面是网上找的解决方案 Zend ...

  7. 双列集合Map的嵌套遍历

    双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...

  8. PostgreSQL Replication之第六章 监控您的设置(4)

    6.4 处理监控工具 还有几个监控工具可以使您的日常生活更轻松. 其中最流行的监控工具是Nagios.它被广泛地使用,也支持各种软件组件. 要使用 Nagios 来监控您的 PostgreSQL 集群 ...

  9. B-Boxes

    http://agc010.contest.atcoder.jp/tasks/agc010_b Problem Statement There are N boxes arranged in a ci ...

  10. caffe(5) 其他常用层及参数

    本文讲解一些其它的常用层,包括:softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层及其它们的参数配置. 1.softmax-loss so ...