BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio>
#include<algorithm>
#define N 50010
#define M 500010
#define rg register
#define LL long long
using namespace std;
int n,m,cnt,root,tot,totnow,totbf;
int last[N],size[N],mxsize[N],dep[N],tmp[N],mg[N];
LL ans=;
bool v[N];
struct edge{
int to,pre,dis;
}e[M];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
void getroot(int x,int fa){
size[x]=; mxsize[x]=;
for(rg int i=last[x],to;i;i=e[i].pre)if(!v[to=e[i].to]&&to!=fa){
getroot(to,x); size[x]+=size[to];
mxsize[x]=max(mxsize[x],size[to]);
}
mxsize[x]=max(mxsize[x],cnt-mxsize[x]);
if(!root||mxsize[x]<mxsize[root]) root=x;
}
void getdep(int x,int fa,int d){
dep[++totnow]=d;
for(rg int i=last[x],to;i;i=e[i].pre)
if(!v[to=e[i].to]&&to!=fa) getdep(to,x,d+e[i].dis);
}
void dfs(int x){
v[x]=; totbf=; tmp[]=;
for(rg int i=last[x],to;i;i=e[i].pre)if(!v[to=e[i].to]){
totnow=; getdep(to,x,e[i].dis); sort(dep+,dep++totnow);
int p=;
for(rg int j=totbf;j;j--){
while(p<=totnow&&tmp[j]+dep[p]<=m) p++;
ans+=p-;
}
p=; int p2=,totmg=;
while(p<=totnow&&p2<=totbf)
mg[++totmg]=dep[p]<tmp[p2]?dep[p++]:tmp[p2++];
while(p<=totnow) mg[++totmg]=dep[p++];
while(p2<=totbf) mg[++totmg]=tmp[p2++];
for(rg int j=;j<=totmg;j++) tmp[j]=mg[j];
totbf=totmg;
}
for(rg int i=last[x],to;i;i=e[i].pre)if(!v[to=e[i].to]){
root=; cnt=size[to];
getroot(to,x); dfs(root);
}
}
int main(){
n=read();
for(rg int i=;i<n;i++){
int u=read(),v=read(),w=read();
e[++tot]=(edge){v,last[u],w}; last[u]=tot;
e[++tot]=(edge){u,last[v],w}; last[v]=tot;
}
m=read();
cnt=n; getroot(,); dfs(root);
printf("%lld\n",ans);
}
BZOJ 1468 Tree 【模板】树上点分治的更多相关文章
- bzoj 1468 Tree(点分治模板)
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1527 Solved: 818[Submit][Status][Discuss] ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
- BZOJ 1468: Tree
Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...
- 【刷题】BZOJ 1468 Tree
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
- POJ 1741:Tree(树上点分治)
题目链接 题意 给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 思路 <分治算法在树的路径问题中的应用> 图片转载于http://www.cnblogs.com/Paul-Gu ...
- BZOJ 3672: [Noi2014]购票 树上CDQ分治
做这道题真的是涨姿势了,一般的CDQ分治都是在序列上进行的,这次是把CDQ分治放树上跑了~ 考虑一半的 CDQ 分治怎么进行: 递归处理左区间,处理左区间对右区间的影响,然后再递归处理右区间. 所以, ...
- HDU 4812:D Tree(树上点分治+逆元)
题目链接 题意 给一棵树,每个点上有一个权值,问是否存在一条路径(不能是单个点)上的所有点相乘并对1e6+3取模等于k,输出路径的两个端点.如果存在多组答案,输出字典序小的点对. 思路 首先,(a * ...
- POJ-1741 Tree (树上点分治)
题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...
随机推荐
- nginx开发(一) 源码-编译
1:获取源码 http://nginx.org/download/nginx-1.8.0.tar.gz 2:编译 解压之后,进入根目录,执行 ./configuer.sh make make inst ...
- spring-boot-configuration-processor的作用
spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 先引入pom依赖 <d ...
- codeforces GYM 100781A【树的直径】
先求出每棵树的直径,排个序,要想图的直径最小的话需要每棵树的直径中点像直径最大的树的直径中点连边,这样直径有三种情况:是直径最大的树的直径:a[tot]:是直径最大的树和直径第二大的树的半径拼起来+1 ...
- bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include< ...
- [App Store Connect帮助]六、测试 Beta 版本(3.1)管理测试员:添加内部测试员
您可以添加至多 25 个内部测试员(您的 App Store Connect 用户)使用“TestFlight Beta 版测试”来测试您的 App.在您上传了至少一个构建版本之后,才可添加测试员. ...
- (快排)51NOD 1018 排序
给出N个整数,对着N个整数进行排序 Input 第1行:整数的数量N(1 <= N <= 50000) 第2 - N + 1行:待排序的整数(-10^9 <= A[i] < ...
- web界面bug-临时
一.登录页面 二.首页 三.项目 四.项目池 五.专家管理 六.审批 七.日/周报 八.设置
- 【hdu多校联考第二场】Odd Shops
Description 这道题的题意是这道难读,大概就是给你n个商店,每个商店的重量为i的商品用ai表示,对于任意商店的a数列都是相同的,重量的范围为[1,10] 求购买方案总数为奇数的重量一共有多少 ...
- AOP面向方面编程---postsharp
PostSharp是一个用于在.NET平台上实现AOP(Aspect-Oriented Programming,面向方面编程)的框架,现通过简单的示例代码来演示如何使用postsharp. 1.新建一 ...
- Previous operation has not finished; run 'cleanup' if it was interrupted.SVN报错
原因: 错误的原因是SVN管理的文件夹改名.删除文件太过频繁,导致有操作挂起了. 解决方式: 1.百度sqlite3.exe并下载. 2.将该文件解压到项目根目录下的.svn文件夹中. 3.打开cmd ...