题面在这里!

挺智障的一个二分。。。我还写了好久QWQ,退役算啦

题解见注释。。。

/*
先对每个点记录 向子树外的最长路 和 向子树内最长路,然后二分。
二分的时候枚举链的LCA直接做就好啦。
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define pb push_back
const int N=100005,B=2333333; inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
} int to[N*2],ne[N*2],val[N*2],num,mid,d[N];
int hd[N],n,m,f[N],dn[N],dw[N],k,ans;
bool flag; inline void add(int x,int y,int z){
to[++num]=y,ne[num]=hd[x],hd[x]=num,val[num]=z;
} void fdfs(int x,int fa){
for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa)
fdfs(to[i],x),dn[x]=max(dn[x],dn[to[i]]+val[i]);
} void sdfs(int x,int fa,int L){
int D=d[x],qz[D+1],hz[D+1];
dw[x]=qz[0]=hz[0]=L; for(int i=hd[x],j=0;i;i=ne[i]) if(to[i]!=fa){
j++,qz[j]=hz[j]=dn[to[i]]+val[i];
} for(int i=1;i<D;i++) qz[i]=max(qz[i],qz[i-1]);
hz[D]=0;
for(int i=D-1;i;i--) hz[i]=max(hz[i],hz[i+1]); for(int i=hd[x],j=0;i;i=ne[i]) if(to[i]!=fa){
j++,sdfs(to[i],x,max(qz[j-1],hz[j+1])+val[i]);
}
} void dfs(int x,int fa){
int M=0,C=0;
for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
dfs(to[i],x);
if(dn[to[i]]+val[i]>mid){
if(!M) M=f[to[i]];
else if(!C) C=f[to[i]];
else{ M=C=k; break;}
}
} f[x]=C?k:M+1;
flag|=(dw[x]<=mid&&M+C<k);
} inline bool solve(){
memset(f,0,sizeof(f)),flag=0;
dfs(1,0); return flag;
} int main(){
n=read(),k=read();
for(int i=1,uu,vv,ww;i<n;i++){
uu=read(),vv=read(),ww=read();
add(uu,vv,ww),add(vv,uu,ww);
d[uu]++,d[vv]++;
} fdfs(1,0),d[1]++,sdfs(1,0,0); int L=0,R=1e9;
while(L<=R){
mid=L+R>>1;
if(solve()) ans=mid,R=mid-1;
else L=mid+1;
} printf("%d\n",ans);
return 0;
}

  

CodeForces - 1004E Sonya and Ice Cream的更多相关文章

  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 686A A. Free Ice Cream(水题)

    题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...

  3. Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心

    题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...

  4. Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)

    网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自https://www.cnblogs.com/qldabiaoge/p/9315722.html. 首先用set数组维护每一个节点所连接的边的 ...

  5. 「CF1004E」Sonya and Ice Cream

    题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...

  6. 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 ...

  7. 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring

    http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...

  8. 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)

    传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...

  9. CodeForces 804C Ice cream coloring

    Ice cream coloring 题解: 这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树). 所以就不会存在多个set+起来之后是一个新的完 ...

随机推荐

  1. 正则表达式实现将html文本转换为纯文本格式(将html字符串转换为纯文本方法)

    Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase); string strOutput = regex. ...

  2. Spring4+SpringMVC+MyBatis整合思路(山东数漫江湖)

    1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...

  3. charles & Fiddle

    一.Charles Charles是在Mac下常用的截取网络封包的工具,在做移动端开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析.Charles通过将自己设置成系统的网络访问 ...

  4. 关于 zfs 命令相关介绍

    三种设备:filesystem  volume  snapshot 1.zfs listroot@UA4300D-spa:~/hanhuakai/pro_07/git_0708# zfs listNA ...

  5. aptitude约等于apt-get的工具

    如题,与之不同的是其会将依赖的程序也给删除. https://baike.baidu.com/item/aptitude/6849487?fr=aladdin 以下是一些常用 aptitude命令,仅 ...

  6. postman测试express restful接口

    安装express及postman var express = require('express') var app = express(); var calculation = require('. ...

  7. Django rest framework 权限操作(源码分析)

    知识回顾http://www.cnblogs.com/ctztake/p/8419059.html 这一篇是基于上一篇写的,上一篇谢了认证的具体流程,看懂了上一篇这一篇才能看懂, 当用户访问是 首先执 ...

  8. 工具===代替cmd的conemu设置

    conemu设置 Win+Alt+P进入设置界面,字体设置: 隐藏右上角菜单和窗口标题. (Ctrl + ~ 隐藏/显示terminal) 设置背景图片 避免误操作,关闭/新建确认 设置win+w默认 ...

  9. x64dbg

    https://x64dbg.com/ https://github.com/x64dbg/x64dbg https://sourceforge.net/projects/x64dbg/files/s ...

  10. vue 同页面不同参数

    项目:详情页中有一个模块为更多产品,点击也是跳转到详情页,也就是相同路由,不同参数. 试过的方法:用this.$router.push,并没有任何反应,没有任何请求,页面也未重新加载,用this.$e ...