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

点分治模板

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N (40000+10)
using namespace std;
struct node
{
int to,next,len;
}edge[N*];
int n,k,sum,root,ans;
int head[N],num_edge;
int depth[N],d[N],size[N],maxn[N];
bool vis[N];
void add(int u,int v,int l)
{
edge[++num_edge].to=v;
edge[num_edge].len=l;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Get_root(int x,int fa)
{
size[x]=; maxn[x]=;
for (int i=head[x];i!=;i=edge[i].next)
if (edge[i].to!=fa && !vis[edge[i].to])
{
Get_root(edge[i].to,x);
size[x]+=size[edge[i].to];
maxn[x]=max(maxn[x],size[edge[i].to]);
}
maxn[x]=max(maxn[x],sum-size[x]);
if (maxn[x]<maxn[root]) root=x;
} void Get_depth(int x,int fa)
{
depth[++depth[]]=d[x];
for (int i=head[x];i!=;i=edge[i].next)
if (edge[i].to!=fa && !vis[edge[i].to])
{
d[edge[i].to]=d[x]+edge[i].len;
Get_depth(edge[i].to,x);
}
} int Calc(int x,int cost)
{
d[x]=cost; depth[]=;
Get_depth(x,);
sort(depth+,depth+depth[]+);
int l=,r=depth[],cnt=;
while (l<r)
if (depth[l]+depth[r]<=k)
cnt+=r-l,l++;
else
r--;
return cnt;
} void Solve(int x)
{
ans+=Calc(x,);
vis[x]=true;
for (int i=head[x];i!=;i=edge[i].next)
if (!vis[edge[i].to])
{
ans-=Calc(edge[i].to,edge[i].len);
sum=size[edge[i].to];
root=;
Get_root(edge[i].to,);
Solve(root);
}
} int main()
{
int u,v,l;
scanf("%d",&n);
sum=maxn[]=n;
for (int i=;i<=n-;++i)
{
scanf("%d%d%d",&u,&v,&l);
add(u,v,l); add(v,u,l);
}
scanf("%d",&k);
Get_root(,);
Solve(root);
printf("%d",ans);
}

BZOJ1468:Tree(点分治)的更多相关文章

  1. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  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. [bzoj1468][poj1741]Tree[点分治]

    可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...

  5. 洛谷4178 BZOJ1468 Tree题解点分治

    点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...

  6. 【点分治】bzoj1468 Tree

    同poj1741. 换了个更快的姿势,不会重复统计然后再减掉什么的啦~ #include<cstdio> #include<algorithm> #include<cst ...

  7. bzoj1468 Tree

    最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...

  8. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  9. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

随机推荐

  1. Swift函数_外部参数名,

    //1________________ /** *没使用外部参数名的函数 */ func inputScore(name:String,score1:Int,score2:Int) { } /** * ...

  2. 基于环境变量为多用户配置不同的JDK(win)

    情景 同一服务器同时部署不同项目需要使用不同的JRE 环境 Windows Server 2008 需求 用户admin 需要使用jdk1.8 用户admin1 需要使用jdk1.7 解决 通过配置用 ...

  3. Oracle数据库基本操作(一) —— Oracle数据库体系结构介绍、DDL、DCL、DML

    一.Oracle数据库介绍 1.基本介绍 Oracle数据库系统是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(CLIENT/SERVER)或B/ ...

  4. JQuery实现获取多个input输入框的值,并存放在一个数组中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 第二天-while循环 格式化输出 运算符 编码

    一.while循环 while 条件: 语句块(循环体)     #判断条件是否成立,若成立执行循环体,然后再次判断条件...直到不满足跳出循环 else: 当条件不成立的时候执行这里,和break没 ...

  6. [SD2015]序列统计——solution

    http://www.lydsy.com/JudgeOnline/problem.php?id=3992 很容易得出DP方程: f[i][c]=f[i-1][a]*f[1][b]① 其中a*b%M=c ...

  7. 有关动态规划(主要是数位DP)的一点讨论

    动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法.20世纪50年代初美国数学家在研究多阶段决策过程的优化问题时, ...

  8. laravel之引入图片上传类

    1.在官网http://www.uploadify.com/ 下载插件,flash verison 的版本是免费版 2.解压后将文件夹放置在指定的目录下 3.前端导入css,js文件,可以仿照文件夹中 ...

  9. 样式 bootstrap purecss Amaze UI 推荐

    Bootstrap 简洁.直观.强悍的前端开发框架,让web开发更迅速.简单. Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAV ...

  10. Grunt入门学习之(2) -- Gruntfile的编写

    Gruntfile由以下几部分构成: "wrapper" 函数 项目与任务,目标配置 加载grunt插件和任务 自定义任务 1.wrapper函数(包装函数) 每一个 Gruntf ...