CodeForces - 1004E Sonya and Ice Cream
挺智障的一个二分。。。我还写了好久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的更多相关文章
- E. Sonya and Ice Cream(开拓思维)
E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforces 686A A. Free Ice Cream(水题)
题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...
- Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心
题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...
- Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)
网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自https://www.cnblogs.com/qldabiaoge/p/9315722.html. 首先用set数组维护每一个节点所连接的边的 ...
- 「CF1004E」Sonya and Ice Cream
题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...
- 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 ...
- 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring
http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...
- 【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 ...
- CodeForces 804C Ice cream coloring
Ice cream coloring 题解: 这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树). 所以就不会存在多个set+起来之后是一个新的完 ...
随机推荐
- Jquery checkbox 遍历
checkbox 全选\全部取消 $("#ChkAll").click(function(){ $("#divContent input[type='checkbo ...
- Android控件——ToggleButton多状态按钮(实现灯泡的开关)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAAFxCAIAAAB7jkm1AAAgAElEQVR4nOy9eXgUVb7/Dy7j3BnH8T
- cf786a
title: CodeForces 786A Berzerk data: 2018-3-3 10:29:40 tags: 博弈论 bfs 无限 with draw copyright: true ca ...
- 10:django 模板语言
Django的模板语言的目的是取得力量和易用性之间的平衡,与其他的模板语言相比,django模板语言显得更简单,更专一, django模板系统由模板,变量,过滤器,标签,注释等主要部分组成 模板 一个 ...
- redis之(九)redis的事务机制
[一]什么是redis的事务 --->redis的事务是一组命令的集合. --->redis的事务是保证一组命令,要么都执行,要么都不执行.但不支持一组命令中,其中一个或多个执行失败,不支 ...
- hdu 2444(染色法判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- poj 3280(区间DP)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7869 Accepted: 38 ...
- matlab基本指令
基本命令 close all //关闭所有figure 命令打开的窗口,在命令窗口输入 clear all //清除之前运行程序所存下的所有变量 size(mat) a = [1 2 3 ; 4 5 ...
- 微信小程序~触摸相关事件(拖拽操作、手势识别、多点触控)
touchstart 手指触摸动作开始 touchmove 手指触摸后移动 touchcancel 手指触摸动作被打断,如来电提醒,弹窗 touchend 手指触摸动作结束 ...
- FFT模板 生成函数 原根 多项式求逆 多项式开根
FFT #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> ...