题面在这里!

挺智障的一个二分。。。我还写了好久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. LTC 钱包部署

    基础环境 系统: CentOS 7.x nodejs: v4.6.0 zeromq: 4.x 安装nodejs + zeromq 基础依赖 yum install -y gcc make gcc-c+ ...

  2. hdu 3371(prim算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...

  3. python模块(requests,logging)

    一.requests Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythone ...

  4. perl多线程tcp端口扫描器(原创)

    perl多线程tcp端口扫描器(原创) http://bbs.chinaunix.net/thread-1457744-1-1.html perl socket 客户端发送消息 http://blog ...

  5. 项目评审ppt的纲要

    1.prd不能模糊,产品的问题全部明确 2.收益在哪里 3.设计体现业务4.怎样保证数据的前后协作5.异常如何处理6.技术解决的痛点7.对外部依赖8.性能指标预期(响应时间)9.

  6. javascript 常用DOM操作整理

    .选取了DOM操作中实用并常用的部分,省略了实用但有明显兼容性的部分2.DOM属性和方法的类型归属可能并不完全准确3.某些一般兼容性和特点做了标识(主要是ie8-9上下) 节点类型 节点类型 节点值 ...

  7. ado中dispose和close的区别,摘自网络

    Close() and Dispose() are basically the same thing on an ADO.NET connection object for providers shi ...

  8. go run/ go install/ go build / go get的区别

    go run 运行当个.go文件 go install 在编译源代码之后还安装到指定的目录 go build 加上可编译的go源文件可以得到一个可执行文件 go get = git clone + g ...

  9. C# 导入Excel

    #region 导入 /// <summary> /// 导入 /// </summary> /// <param name="sender"> ...

  10. Python基础系列----序列(列表、元组、字符串)

    1.定义                                                                                               1 ...