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. ZOJ 3369 Saving Princess

    Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  2. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  3. OKHttp使用简单介绍

    如今android网络方面的第三方库非常多,volley.Retrofit.OKHttp等,各有各自的特点,这边博客就来简介下怎样使用OKHttp. 梗概 OKHttp是一款高效的HTTP客户端,支持 ...

  4. SpringMVC+Jquery -页面异步载入数据

    背景: 做项目时涉及到页面.当我打算在controller中传一个list到页面,然后通过<c:foreach>循环遍历出来时,同事说:你这样每次都要刷新.这都是几百年前使用的技术了.你用 ...

  5. 一、奇妙插件Tampermonkey的简单安装教程

    奇妙插件Tampermonkey的简单安装教程 1.下载 对于浏览器而言,一般的功能并不能满足"特殊用户的需求".这时候就须要插件来帮忙了.那么讲到插件的支持多又常见的浏览器必定要 ...

  6. C# 解压及压缩文件源代码

    using System.IO; using System.Windows.Forms; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.Sh ...

  7. 【RQNOJ】460 诺诺的队列

    [题目大意] 求全部数对(i,j)满足随意a[k]<=a[i]且a[k]<=a[j]. 形象地说,就是有一群人站成一列.每一个人有一定的身高,然后问有多少对人能够互相看得到. 把数对(i, ...

  8. [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...

  9. WebApp之H5登录注册

    代码indexhtml <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  10. session 存入 memcahce

    <?php header('content-type:text/html;charset=utf-8'); class RedisSessionHandler{ public $ttl; //失 ...