BZOJ2051 : A Problem For Fun
树的点分治,将点分治的过程记录下来,每一个分治结构按到分治中心的距离维护所有点。
对于一个点二分答案,然后在$O(\log n)$个分治结构中二分查找,时间复杂度$O(n\log^3n)$。
#include<cstdio>
#include<algorithm>
const int N=50010,M=1000010;
int n,m,i,x,y,z,g[N],nxt[N<<1],v[N<<1],w[N<<1],ok[N<<1],ed=1;
int son[N],f[N],size,now,cnt;
int G[N],NXT[M],V[2][M],W[M],ED,rl[N],rr[N],el[M],er[M],q[M<<1],tot;
inline void add(int x,int y,int z){v[++ed]=y,w[ed]=z,nxt[ed]=g[x],ok[ed]=1,g[x]=ed;}
inline void ADD(int x,int y,int z,int w){V[0][++ED]=y;V[1][ED]=z;W[ED]=w;NXT[ED]=G[x];G[x]=ED;}
void findroot(int x,int pre){
son[x]=1;f[x]=0;
for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=pre){
findroot(v[i],x);
son[x]+=son[v[i]];
if(son[v[i]]>f[x])f[x]=son[v[i]];
}
if(size-son[x]>f[x])f[x]=size-son[x];
if(f[x]<f[now])now=x;
}
void dfs(int x,int pre,int dis){
q[++tot]=dis;
for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=pre)dfs(v[i],x,dis+w[i]);
}
void dfs2(int x,int pre,int dis){
ADD(x,now,cnt,dis);
q[++tot]=dis;
for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=pre)dfs2(v[i],x,dis+w[i]);
}
void solve(int x){
int i;
q[rl[x]=++tot]=0;
for(i=g[x];i;i=nxt[i])if(ok[i])dfs(v[i],x,w[i]);
std::sort(q+rl[x],q+tot+1);
rr[x]=tot;
for(i=g[x];i;i=nxt[i])if(ok[i]){
el[++cnt]=tot+1;
dfs2(v[i],x,w[i]);
std::sort(q+el[cnt],q+tot+1);
er[cnt]=tot;
}
for(i=g[x];i;i=nxt[i])if(ok[i])ok[i^1]=0,f[0]=size=son[v[i]],findroot(v[i],now=0),solve(now);
}
inline int ask(int L,int r,int x){
int l=L,t=l-1,mid;
while(l<=r)if(q[mid=(l+r)>>1]<=x)l=(t=mid)+1;else r=mid-1;
return t-L+1;
}
inline int query(int x,int k){
int t=ask(rl[x],rr[x],k)-1;
for(int i=G[x];i;i=NXT[i])t+=ask(rl[V[0][i]],rr[V[0][i]],k-W[i])-ask(el[V[1][i]],er[V[1][i]],k-W[i]);
return t;
}
inline int getans(int x){
int l=1,r=10000*(n-1),mid;
while(l<r)if(query(x,mid=(l+r)>>1)<m)l=mid+1;else r=mid;
return l;
}
int main(){
scanf("%d%d",&n,&m);
for(i=1;i<n;i++)scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,z);
f[0]=size=n;findroot(1,now=0);solve(now);
for(i=1;i<=n;i++)printf("%d\n",getans(i));
return 0;
}
BZOJ2051 : A Problem For Fun的更多相关文章
- [BZOJ2051]A Problem For Fun/[BZOJ2117]Crash的旅游计划/[BZOJ4317]Atm的树
[BZOJ2051]A Problem For Fun/[BZOJ2117]Crash的旅游计划/[BZOJ4317]Atm的树 题目大意: 给出一个\(n(n\le10^5)\)个结点的树,每条边有 ...
- BZOJ2051——A Problem For Fun
0.题意:给出一个N个结点的树,每条边有一个正整数权值,定义两个结点的距离为连接这两个结点路径上边权的和.对于每个结点i,它到其他N-1个结点都有一个距离,将这些距离从小到大排序,输出第K个距离. 1 ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
随机推荐
- SSH 内网端口转发实战
导读 大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多.不过其实除了这个功能,它的隧道转发功能更是吸引人. 如果两个内网之间的linux服务器需要互相登录,或需要互相访问内网某个端口,担忧 ...
- ubuntu 快速安装jre
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-i ...
- android获取手机信息大全
IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...
- 通过Java反射来理解泛型的本质
集合框架中经常会使用泛型指定集合中所存放元素的类型,保证集合的统一性,从集合中取出元素的时候也避免了类型强制转换的操作,所以我们使用常规的方式来往集合中存放元素的时候,如果指定泛型,那么我们只能向集合 ...
- ubuntu 13.10 amd64安装ia32-libs
很多软件只有32位的,有的依赖32位库还挺严重的:从ubuntu 13.10已经废弃了ia32-libs,但可以使用多架构,安装软件或包apt-get install program:i386.有的还 ...
- UVALive 7261 Xiongnu's Land (扫描线)
Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...
- 获取4G以上的文件大小
1.DWORD dwFileSizeHigh; // 得到文件大小的高位 __int64 qwFileSize = GetFileSize(m_hSrcBigFile, &dwFileSi ...
- Liz Murray成功故事的偶然与必然(转)
偶尔,我看到了一部电影:Homeless to Harvard (风雨哈佛路) 说句老实话,她( Liz Murray 莉丝·默里 )的经历确实让人钦佩和学习! 下面我看到他人写的有这本电影的评论,觉 ...
- margin和padding那点事及常见浏览器margin padding相关Bug
用Margin还是用Padding 何时应当使用margin: 需要在border外侧添加空白时. 空白处不需要背景(色)时. 上下相连的两个盒子之间的空白,需要相互抵消时.如15px + 20px的 ...
- 10.组合模式(Composite Pattern)
using System; using System.Collections.Generic; namespace ConsoleApplication8 { class Program { stat ...