#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 【模板】树上点分治的更多相关文章

  1. bzoj 1468 Tree(点分治模板)

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1527  Solved: 818[Submit][Status][Discuss] ...

  2. BZOJ.1468.Tree(点分治)

    BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...

  3. BZOJ 1468: Tree

    Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...

  4. 【刷题】BZOJ 1468 Tree

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  5. bzoj 1468 Tree 点分

    Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1972  Solved: 1101[Submit][Status][Discuss] Desc ...

  6. POJ 1741:Tree(树上点分治)

    题目链接 题意 给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 思路 <分治算法在树的路径问题中的应用> 图片转载于http://www.cnblogs.com/Paul-Gu ...

  7. BZOJ 3672: [Noi2014]购票 树上CDQ分治

    做这道题真的是涨姿势了,一般的CDQ分治都是在序列上进行的,这次是把CDQ分治放树上跑了~ 考虑一半的 CDQ 分治怎么进行: 递归处理左区间,处理左区间对右区间的影响,然后再递归处理右区间. 所以, ...

  8. HDU 4812:D Tree(树上点分治+逆元)

    题目链接 题意 给一棵树,每个点上有一个权值,问是否存在一条路径(不能是单个点)上的所有点相乘并对1e6+3取模等于k,输出路径的两个端点.如果存在多组答案,输出字典序小的点对. 思路 首先,(a * ...

  9. POJ-1741 Tree (树上点分治)

    题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...

随机推荐

  1. 22.Extjs Panel中显示多行工具栏(tbar)

    转自:http://blog.sina.com.cn/s/blog_454fbf740100t0xj.html 在应用程序的制作中,我们经常性的会用到工具栏,在Extjs中Panel中提供了tbar和 ...

  2. Tomcat根目录下work文件夹的作用(转载)

    用 tomcat作web服务器的时候,部署的程序在webApps下,这些程序都是编译后的程序.tomcat有一个work目录,里面存放了页面的缓存, 访问的jsp都会编译,编译后的文件都会存储在wor ...

  3. spring的annotation

    spring容器创建bean对象的方式: 1,使用反射调用无参构造器来创建实例(前提是这个类有无参构造器)(常规方式) 2,通过工厂类获得实例(工厂类实现了接口FactoryBean<?> ...

  4. ACM_校庆素数

    校庆素数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 广财建校33年了,如今迎来了她的校庆. 小财最近想在研究素数,她突发奇想 ...

  5. 299 Bulls and Cows 猜数字游戏

    你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...

  6. Spring-security自定义过滤器

    定义过滤器 public class TokenAuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter { publ ...

  7. 窗体基础WINFORM

    winform 1.窗体: 造窗体界面: 窗体设计界面: 窗体类名不能重复! 属性: acceptbutton:回车是默认点击按钮 cancelbutton:按esc按键式默认的按钮 backcolo ...

  8. mysql若干问题

    一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...

  9. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  10. vscode使用教程(web开发)

    1.安装 进入官网下载https://code.visualstudio.com/ 一直下一步就好了,中间可以选择把软件安装在哪个目录. 2.常用插件安装 a. 进入扩展视图界面安装/卸载 a1.快捷 ...