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

Solution

开始专做点分治的题目了

这就是点分治的裸题了吧

找到重心并分治之后,维护的是与根的距离

对于每个根,求的是路径经过根的两点距离小于 \(k\) 的方案数

注意去重

calc中如果有两点在同一子树内,那么它们在当前calc中信息是假的(它们本身之间的距离并没有那么大),所以在访问这个子树之前要先把多算的减掉

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=40000+10,inf=0x3f3f3f3f;
int n,k,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],deep[MAXN],cnt,Msonsize[MAXN],size[MAXN],d[MAXN],root,finish[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
w[e]=z;
}
inline void getroot(int x,int f,int ntotal)
{
Msonsize[x]=0;size[x]=1;
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f||finish[to[i]])continue;
else
{
getroot(to[i],x,ntotal);
size[x]+=size[to[i]];
chkmax(Msonsize[x],size[to[i]]);
}
chkmax(Msonsize[x],ntotal-size[x]);
if(Msonsize[x]<Msonsize[root])root=x;
}
inline void getdeep(int x,int f)
{
deep[++cnt]=d[x];
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f||finish[to[i]])continue;
else d[to[i]]=d[x]+w[i],getdeep(to[i],x);
}
inline int calc(int x,int st)
{
d[x]=st;cnt=0;
getdeep(x,0);
std::sort(deep+1,deep+cnt+1);
int l=1,r=cnt,ans=0;
while(l<r)
{
if(deep[l]+deep[r]<=k)ans+=r-l,l++;
else r--;
}
return ans;
}
inline int solve(int x)
{
int res=calc(x,0);
finish[x]=1;
for(register int i=beg[x];i;i=nex[i])
if(!finish[to[i]])
{
res-=calc(to[i],w[i]);
root=0;
getroot(to[i],x,size[to[i]]);
res+=solve(root);
}
return res;
}
int main()
{
read(n);
for(register int i=1;i<n;++i)
{
int u,v,w;
read(u);read(v);read(w);
insert(u,v,w);insert(v,u,w);
}
read(k);
Msonsize[0]=inf;root=0;
getroot(1,0,n);
write(solve(root),'\n');
return 0;
}

【刷题】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 点分

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

  3. BZOJ 1468: Tree

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

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

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

  5. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

  6. [刷题] 102 Binary Tree Level Order Traversal

    要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...

  7. BZOJ 1468 Tree 【模板】树上点分治

    #include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...

  8. [刷题] PTA 03-树3 Tree Traversals Again

    用栈实现树遍历 1 #include<stdio.h> 2 #include<string.h> 3 #define MAXSIZE 30 4 5 int Pre[MAXSIZ ...

  9. [刷题] 257 Binary Tree Paths

    要求 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串 示例 ["1->2->5","1->3"] 思路 递归地返回左右子树到叶子节 ...

随机推荐

  1. 【MYSQL命令】mysql基础命令

    1.查询MYSQL当前用户命令:select user() mysql> select user(); +------------------------+ | user() | +------ ...

  2. Spark Shell Examples

    Spark Shell Example 1 - Process Data from List: scala> val pairs = sc.parallelize( List( ("T ...

  3. Halcon算子解释

    Halcon算子解释大全 Halcon/Visionpro视频教程和资料,请访问 重码网,网址: http://www.211code.com Chapter 1 :Classification 1. ...

  4. IBM基于Kubernetes的容器云全解析

    基于Kubernetes的容器云 容器云最主要的功能是以应用为中心,帮助用户把所有的应用以容器的形式在分布式里面跑起来,最后把应用以服务的形式呈现给用户.容器云里有两个关键点,一是容器编排,二是资源调 ...

  5. Scrum立会报告+燃尽图(Beta阶段第二次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 项目地址:https://coding.net/u/wuyy694 ...

  6. 软件工程-东北师大站-第四次作业PSP

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  7. DP---(POJ1159 POJ1458 POJ1141)

    POJ1159,动态规划经典题目,很适合初学者入门练手. 求:为了使字符串左右对称,应该插入的最小字符数目. 设字符串为S1 S2 S3 - Sn. 这个字符串有n个字符,根据DP的基本思路,减少问题 ...

  8. lintcode-488-快乐数

    488-快乐数 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为 ...

  9. QMdiArea及QMdiSubWindow实现父子窗口及布局方法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法     本文地址:http ...

  10. 上传web端——个人项目

    我用visual studio新建了一个web窗口,如图: 然后这里是系统自带的代码: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile ...