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+起来之后是一个新的完 ...
随机推荐
- 【BZOJ】1666 [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
[算法]贪心&&堆 [题解]反过来看就是合并任意两块木板,花费为木板长度之和. 显然从最小的两块开始合并即可,用堆(优先队列)维护. 经典DP问题石子归并是只能合并相邻两堆石子,所以不 ...
- Apache2.4+Tomcat7 集群
Apache2.4+Tomcat7 集群: 1.下载并安装相对应的软件 apache下载地址:http://httpd.apache.org/ 这里使用apache2.4 tomcat下载地址:htt ...
- nodewebkitV0.21.6版本的学习
1.下载 https://nwjs.io/ 建议下载sdk版本,有调试功能,正式发布时,使用normal版本 下载以后解压,得到的文件目录如下 点击nw.exe,显示如下 2.hello world ...
- JavaScript match() 方法
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. var st ...
- 【LOJ6201】【bzoj4939】【YNOI2016】掉进兔子洞
一道比较简单的莫队…… 用bitset维护三个区间的交元素. #include<bits/stdc++.h> ; ; #define UI unsigned int #define rep ...
- #error This file was generated by a newer version of protoc
pattern@pattern89:/raid0/workspace/houjun/caffe-ssd$ sudo make all -j8PROTOC src/caffe/proto/caffe.p ...
- [New learn] 手势
1.简介 我们经常会在设备上查看图片等, 也会经常将图片通过手指的捏合打开来缩小和方法图片.这就是ios中的手势功能在起作用. 那么手势好像也是一种touch事件,那和UIResponder中定义的t ...
- web中的CSS、Xpath等路径定位方法学习
今天不到八点就到公司了,来的比较早,趁着有点时间,总结下web中的CSS.Xpath等路径定位定位的方式吧! 简单的介绍下xpath和css的定位 理论知识就不罗列了 还是利用博客园的首页.直接附上代 ...
- 2017百度春招<度度熊买帽子的问题>
题目: 度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同.度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少? 数组中找到第三小的数字 注意边界条件 用STL中的set来 ...
- Longest Valid Parentheses——仍然需要认真看看(动态规划)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...