题面在这里!

挺智障的一个二分。。。我还写了好久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. Spring mvc详解(山东数漫江湖)

    Spring mvc框架 Spring web MVC 框架提供了模型-视图-控制的体系结构和可以用来开发灵活.松散耦合的 web 应用程序的组件.MVC 模式导致了应用程序的不同方面(输入逻辑.业务 ...

  2. js 合并多个对象 Object.assign

    Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. var o1 = { a: 1 };var o2 = { b: 2 };var o3 ...

  3. php中的parse_ini_file函数

    作用:parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置 格式:parse_ini_file(file,true)// (第二个参数为可选参数.如果设置为 true, ...

  4. (二十一)Makefile例子

    ROOT_PROJECT = .DIR_INC = -I$(ROOT_PROJECT)/include -I$(ROOT_PROJECT)/include/NE10 DIR_BIN = $(ROOT_ ...

  5. gnu app url[web][5星]

    http://www.gnu.org/software/software.zh-cn.html http://linux.chinaunix.net/news/2010/12/07/1175310.s ...

  6. EasyHook远程进程注入并hook api的实现

    EasyHook远程进程注入并hook api的实现 http://blog.csdn.net/v6543210/article/details/44276155

  7. ERROR 1682 (HY000)

    ERROR 1682 (HY000) xtrabackup 恢复数据库后,出现1682错: root@localhost [(none)]>show global variables like ...

  8. react项目中遇到的一些问题

    推荐使用facebook官方构建工具create-react-app来创建React基础工程.(然而我还是手动构建) (路由)官方旧版本和V4的比较.https://github.com/ReactT ...

  9. nginx配置文件的详细讲解

    user nginx nginx; #定义Nginx运行的用户和用户组worker_processes 1; #nginx进程数,建议设置为等于CPU总核心数worker_rlimit_nofile ...

  10. hive-group by的时候把两个字段变成map

    源表结构: pcgid               string mobilegid           string value               double 测试数据如下: p1 m1 ...