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. javascript位操作符右移>>>的妙用

    var len=arr.length>>>0; 在arr.length为null或undefined的时间,强制转换为0;

  2. root用户删除文件提示:Operation not permitted

    root用户删除文件提示:Operation not permitted http://blog.csdn.net/evanbai/article/details/6187578

  3. cocos2dx 使用spine制作骨骼动画

    刚刚接触骨骼动画,所以写一篇文章记录. 1.首先先画好人物的每一个部件: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fon ...

  4. [MST] Attach Behavior to mobx-state-tree Models Using Actions

    Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...

  5. unix中文件I/O

    在unix中可用的文件I/O函数包含打开文件,读文件,写文件等. Unix系统中的大多数文件I/O须要用到5个函数:open,read,write,lseek,close. 这里要说明的是read,w ...

  6. how to deal with &quot;no such file error or diretory&quot; error for a new programmer in QT creator

    when i try to develop a hello demo in QT creator with the code following : #include<QApplication& ...

  7. 开源 java CMS - FreeCMS2.3会员个人资料

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/28577.html​ 项目地址:http://www.freeteam.cn/ 个人资料 从 ...

  8. [JZOJ4274] [NOIP2015模拟10.28B组] 终章-剑之魂 解题报告(二进制)

    Description [背景介绍]古堡,暗鸦,斜阳,和深渊……等了三年,我独自一人,终于来到了这里……“终焉的试炼吗?就在这里吗?”我自言自语道.“终焉的试炼啊!就在这里啊!”我再一次自言自语道.“ ...

  9. POJ 1151 线段树+扫描线

    题意:求矩形面积的并 思路: 注意是[l,mid][mid,r] 这是真正的线段了 就当扫描线模板使吧~ //By SiriusRen #include <cmath> #include ...

  10. C++中inline函数

    (一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...