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 ...
随机推荐
- 杂项-公司:Sun
ylbtech-杂项-公司:Sun Sun Microsystems是IT及互联网技术服务公司(已被甲骨文收购)Sun Microsystems 创建于1982年.主要产品是工作站及服务器.1986年 ...
- Hadoop 分布式环境slave节点重启忽然不好使了
Hadoop 分布式环境slaves节点重启: 忽然无法启动DataNode和NodeManager处理: 在master节点: vim /etc/hosts: 修改slave 节点的IP (这个时候 ...
- bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名【bitset+Floyd传递闭包】
把关系变成有向边,稍微想一下就是要求在有向图中不能到达的点对个数,这个可以用Floyd传递闭包来做,但是n^3的复杂度跑不了1000 考虑bitset优化! 因为传递过程只会出现0和1,用bitset ...
- 清北考前刷题day1早安
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...
- 慕课网4-6 编程练习:jQuery后排兄弟选择器
4-6 编程练习 结合所学的兄弟选择器" ~ ",实现如下图所示效果: 任务 (1)使用兄弟选择器" ~ "将技术语言的背景色变成红色 (2)使用jQuery的 ...
- Androidstudio的安装与使用调试
1安装与基本使用 1.1androidstudio的安装 1.到android-studio\bin文件夹里面,根据自己的电脑配置,打开studio.exe或者studio64.exe 2.按照向导默 ...
- C基础-对malloc的使用与理解
一.malloc函数分析 1.函数原型 void * malloc(size_t size); 2.Function(功能) Allocates a block of size bytes of m ...
- [转]MySQL存储过程
转自:http://www.cnblogs.com/exmyth/p/3303470.html 14.1.1 创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE ...
- 获取Sprite上某一个点的透明度
转载[ http://www.cnblogs.com/Androider123/p/3795050.html] 本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要 ...
- IPython、Notebook、qtconsole使用教程
IPython.Notebook.qtconsole使用教程 上一篇为Python,IPython,qtconsole,Notebook,Jupyter快速安装教程 1. 使用IPython 自动补全 ...