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. Spring.Net---2、IoC/DI基本概念

    ---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...

  2. 使用Docker镜像部署ELK日志系统

    使用Docker部署elasticsearch.logstash.kibana 指定版本:6.7.1 (建议使用同一的版本.屏蔽三个软件间的不兼容性) 下载镜像: docker pull elasti ...

  3. [模板]选择排序&&冒泡排序&&插入排序

    #include<iostream> #include<cstdio> #include<bits/stdc++.h> using namespace std; v ...

  4. TCP/IP Socket通信demo

    一个实例通过client端和server端通讯 客户端发送:“我是客户端,请多关照” 服务端回复:“收到来自于"+s.getInetAddress().getHostName()+" ...

  5. vi 编辑器使用中常见的命令

    原创作品,转载请在文章头部(显眼位置)注明出处:https://www.cnblogs.com/sunshine5683/p/10014724.html 要在linux学习的路上越走越远,总结是必不可 ...

  6. HDFS 的运行机制

    hdfs haddop distributed system 由 name node, secondary name node,data node, client 组成. 真正存放数据的就是 data ...

  7. bootStrap下拉菜单 点击下拉列表某个元素,列表不隐藏

    html: <a class="dropdown-toggle bgImg-priceWran " id="dropdownMenu1" data-tog ...

  8. QQ 聊天机器人小薇 2.1.0 发布!

    本次发布加入了支持茉莉机器人,并且更容易搭建开发环境,在线显示登录二维码~ 简介 XiaoV(小薇)是一个用 Java 写的 QQ 聊天机器人 Web 服务,可以用于社群互动: 监听多个 QQ 群消息 ...

  9. MvcForum作者称该项目进入缓慢更新

    MvcForum作者在github上发表 This project is no longer actively developed as I don't have the time. As and w ...

  10. django开发博客(1) 入门

    现在正式开始博客开发 1.安装django1.4 如果你使用的是fedoraDVD版,安装时选择了web开发组建,这一步可以省略,因为它自带django环境 django下载地址 https://ww ...