Description

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

Input

N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k

Output

一行,有多少对点之间的距离小于等于k

Sample Input

7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10

Sample Output

5
 
又是一道点分治的题,方法同聪聪可可。
 #include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int deep[N],head[N],son[N],f[N],d[N],root,ans,n,sum,cnt,k;
bool vis[N];
struct ee{int to,next,w;}e[N*];
void insert(int u,int v,int w){
e[++cnt].to=v;e[cnt].next=head[u];e[cnt].w=w;head[u]=cnt;
} void getroot(int x,int fa){
son[x]=;f[x]=;
for (int i=head[x];i;i=e[i].next){
int v=e[i].to;
if (vis[v]||v==fa) continue;
getroot(v,x);
son[x]+=son[v];
f[x]=max(f[x],son[v]);
}
f[x]=max(f[x],sum-f[x]);
if (f[x]<f[root]) root=x;
} void getdeep(int x,int fa){
deep[++deep[]]=d[x];
for (int i=head[x];i;i=e[i].next){
int v=e[i].to;
if (vis[v]||v==fa) continue;
d[v]=d[x]+e[i].w;
getdeep(v,x);
}
} int cal(int x,int now){
int t=;
d[x]=now;deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int l=,r=deep[];
for (;l<r;) if (deep[l]+deep[r]<=k){t+=r-l;l++;}
else r--;
return t;
} void work(int x){
ans+=cal(x,);
vis[x]=;
for (int i=head[x];i;i=e[i].next){
int v=e[i].to;
if (!vis[v]) {
ans-=cal(v,e[i].w);sum=son[v];root=;
getroot(v,);
work(root);
}
}
} int main(){
scanf("%d",&n);
int u,v,w;
for (int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
insert(v,u,w);
}
f[]=sum=n;
scanf("%d",&k);
getroot(,);
work(root);
printf("%d",ans);
}

【BZOJ1468】Tree的更多相关文章

  1. 点分治【bzoj1468】 Tree

    点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...

  2. 【BZOJ-1468】Tree 树分治

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

  3. 【BZOJ1468】Tree [点分治]

    Tree Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...

  4. 【POJ3237】Tree 树链剖分+线段树

    [POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...

  5. 【BZOJ】【2631】Tree

    LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...

  6. 【Luogu1501】Tree(Link-Cut Tree)

    [Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...

  7. 【BZOJ3282】Tree (Link-Cut Tree)

    [BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...

  8. 【AtCoder3611】Tree MST(点分治,最小生成树)

    [AtCoder3611]Tree MST(点分治,最小生成树) 题面 AtCoder 洛谷 给定一棵\(n\)个节点的树,现有有一张完全图,两点\(x,y\)之间的边长为\(w[x]+w[y]+di ...

  9. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

随机推荐

  1. The message queue

    今晚来整理一下消息队列,消息队列是一条由消息连接而成的链表,存在内核里面,通过消息对了的引用标识符来访问,每个消息队列都有一个msqid_ds结构与之对应,这个结构保存了消息队列的当前状态参数,这个结 ...

  2. C# 委托的几种写法

    class Program    {         delegate string Get(string ss);        delegate string Get2(int i);       ...

  3. GD库使用小结---2

    接着上一篇.GD库可以折腾很多用法出来,当然得跟画图相关,除了前面的验证码.水印外,还可以进行图片的缩放,裁剪.旋转等操作,这在很多应用中可以见到. 1. 加水印 前面已经知道,我们可以使用image ...

  4. Linux C编程--fork()详解

    以下给出说明: fork函数 #include <sys/types.h>  #include <unistd.h> pid_t fork(void); fork调用失败则返回 ...

  5. 基于Tengine的反向代理详细配置

    系统环境: SUSE Linux Enterprise Server 10 SP1 (x86_64) 注:所有软件包都放置在/data/software目录下 nginx_tcp_proxy_modu ...

  6. Xcode-GitHub第三方库管理工具--CocoaPods

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库(从一个坑出来,又 ...

  7. ckrule规则编辑器在wpf中的使用

    当前,ckrule的IDE和业务管理系统都是由winform开发的,规则编辑器也只提供了winform的版本,所以很多的朋友都提出意见,要有wpf的版本.wpf的界面设置和管理都更加的方便. 事实上可 ...

  8. (转)Spark安装与学习

    摘要:Spark是继Hadoop之后的新一代大数据分布式处理框架,由UC Berkeley的Matei Zaharia主导开发.我只能说是神一样的人物造就的神器,详情请猛击http://www.spa ...

  9. JavaScript最佳实践:可维护性

    代码约定 一.可读性 代码缩进 包含注释 二.变量和函数命名 变量名应为名词如car或person 函数名应该以动词开始,如getName().返回布尔类型值的函数一般以is开头,如isEnable( ...

  10. Bitmap、CBitmap、HBITMAP以及BITMAP的相互转换

    Bitmap.CBitmap.HBITMAP以及BITMAP的相互转换 构建CBitmapCBitmap bmp;bmp.LoadBitmap(ID); 构建HBITMAPHBitmap = (HBI ...