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. 禁止选中页面内容-兼容ie、firefox、chrome

    使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart, 比如: 在body中加入<body onselectstart="return fa ...

  2. [javaSE] IO流(FIle对象递归文件列表)

    获取File对象,new出来,构造参数:String目录名 调用File对象的list()方法,获取String[]数组文件名称 循环数组,列出所有文件包含隐藏文件 递归列出所有的数据 定义一个静态方 ...

  3. JAVA基础之——数据结构

    JAVA数据结构有8种,如下所示,本文从使用场景,优缺点方面讲解. 1 数组Array ArrayList 使用场景:有序的存储同一类型数据的集合,固定大小 优点:通过索引查找方便 缺点:插入或删除一 ...

  4. uestc Another LCIS

    Another LCIS Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 193 Tried: 2428 Description For a se ...

  5. 02.php面向对象——构造方法&析构方法

    <?php //自己写的构造方法 class Computer{ public function Computer(){ echo '构造方法'; } } new Computer();//这样 ...

  6. 【Win32】对指定进程进行禁音

    使用例子如下: CAudioMgr AudioMgr; AudioMgr.SetProcessMute(GetCurrentProcessId()); H文件的内容如下: #pragma once # ...

  7. (PowerShell) 重命名文件

    Get-ChildItem -Path C:\temp\test -Filter *.txt | Rename-Item -NewName {$_.Basename.Replace("Old ...

  8. Teamviewer 手机端怎么使用右键-已解决

    Teamviewer 手机端怎么使用右键 可能很多人和我一样,关掉了那个提示,然后记不住操作 怎么控制电脑使用右键? 长按就可以了 拖到指定地点,然后长按不送,就会出来电脑右键菜单

  9. Android深入理解Context(三)Context完全解析

      1· Context类型        我们知道,Android应用都是使用Java语言来编写的,那么大家可以思考一下,一个Android程序和一个Java程序,他们最大的区别在哪里?划分界限又是 ...

  10. android头像上传(获取头像加剪切)

    因为项目中需要用到头像上传的功能,所以就下个Ddmo先来实现下. demo我是类似仿微信的,在一个GridView中展示所有的图片,其中第一个item可以去照相:获取到图片后再进行剪切. 图片的剪切是 ...