题目描述

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

输入输出格式

输入格式:

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

输出格式:

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

输入输出样例

输入样例#1:

7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
输出样例#1:

5



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int read() {
int res=;char ch=getchar();
while(!isdigit(ch)) ch=getchar();
while(isdigit(ch)) res=(res<<)+(res<<)+(ch^),ch=getchar();
return res;
}
#define reg register
#define N 200005
int n, tot, k;
long long ans; int head[N], cnt = ;
struct edge {
int nxt, to, val;
}ed[N*];
inline void add(int x, int y, int z)
{
ed[++cnt] = (edge){head[x], y, z};
head[x] = cnt;
} bool cut[N*]; int siz[N], root, mrt = 1e9;
void dfs(int x, int fa)
{
siz[x] = ;
for (reg int i = head[x] ; i ; i = ed[i].nxt)
{
int to = ed[i].to;
if (to == fa or cut[i]) continue;
dfs(to, x);
siz[x] += siz[to];
}
}
void efs(int x, int fa)
{
int tmp = tot - siz[x];
for (reg int i = head[x] ; i ; i = ed[i].nxt)
{
int to = ed[i].to;
if (to == fa or cut[i]) continue;
efs(to, x);
tmp = max(tmp, siz[to]);
}
if (tmp < mrt) mrt = tmp, root = x;
}
inline int FindRoot(int x)
{
return x;
dfs(x, );
mrt = 1e9;
tot = siz[x];
root = n;
efs(x, );
return root;
} int a[N];
int top;
void Work(int x, int fa, int d)
{
a[++top] = d;
for (reg int i = head[x] ; i ; i = ed[i].nxt)
{
int to = ed[i].to;
if (to == fa or cut[i]) continue;
Work(to, x, d + ed[i].val);
}
}
inline int Calc(int x, int d)
{
int res = ;
top = ;
Work(x, , d);
sort (a + , a + + top);
int l = , r = top;
while (l < r)
{
while(a[l] + a[r] > k and l < r) r--;
res += r - l, l ++;
}
return res;
}
void solve(int rt)
{
root = FindRoot(rt);
ans += Calc(root, );
for (reg int i = head[root] ; i ; i = ed[i].nxt)
{
int to = ed[i].to;
if (cut[i]) continue;
cut[i] = cut[i ^ ] = ;
ans -= Calc(to, ed[i].val);
solve(to);
}
} int main()
{
n = read();
for (reg int i = ; i < n ; i ++)
{
int x = read(), y = read(), z = read();
add(x, y, z), add(y, x, z);
} k = read();
solve();
printf("%d\n", ans);
return ;
}

Tree 点分治的更多相关文章

  1. 【BZOJ-1468】Tree 树分治

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

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

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

  3. POJ 1741 Tree 树分治

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

  4. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  5. 【CF434E】Furukawa Nagisa's Tree 点分治

    [CF434E]Furukawa Nagisa's Tree 题意:一棵n个点的树,点有点权.定义$G(a,b)$表示:我们将树上从a走到b经过的点都拿出来,设这些点的点权分别为$z_0,z_1... ...

  6. POJ 1741 Tree(点分治点对<=k)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  7. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  8. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  9. CF1039D You Are Given a Tree 根号分治,贪心

    CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...

  10. BZOJ1468:Tree(点分治)

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

随机推荐

  1. MOOC 数据库系统笔记(一):初步认识数据库系统

    概述 什么是数据库 数据库是电子化信息的集合 数据库起源于规范化"表(Table)"的处理. Table:以按行按列形式组织及展现的数据. E.F.Codd,基于对"表( ...

  2. python小基础

    1.计算机基础知识 中央处理器 CPU 人的大脑 内存 缓存数据 临时记忆 硬盘 储存数据 永久记忆 什么是操作系统 ? 控制计算机工作的流程 什么是应用程序? 安装在操作系统之上的软件 2.pyth ...

  3. BASLER Pylon 抓取策略

    BASLER Pylon 抓取策略 ( 涛哥工业技术 微信号 TaoRobotics) One by One Grab Strategy 逐个抓取策略 当使用 one by one 抓取模式时,图像按 ...

  4. 【linux】【jenkins】jenkins构建、mvn或者npm打包、docker运行、失败自动回滚脚本

    小白对jenkins运维的使用有点简单的想法,这里开个记录贴记录下. 由于未找到jenkins构建失败后执行其他脚本的插件,也暂时没有使用其他运维工具.所以想自己写一个shell脚本,一是方便其他人使 ...

  5. Django模板语言, 过滤器整理

    Django模板语言,过滤器整理 1. add {{ value|add:"2" }} 把add后的参数加给value: 处理时,过滤器首先会强制把两个值转换成Int类型. 如果强 ...

  6. FlagCounter被封杀?自己实现一个简单的多国访客计数器

    起因 前段时间发现博客右边的FlagCounter计数器突然没了,又看到了博客园封杀了FlagCounter的消息,有点摸不着头脑.于是上FlagCounter的网站上看了一眼,发现最近出现的来自新国 ...

  7. 大文件分割、命令脚本 - Python

    日志文件分割.命名 工作中经常会收到测试同学.客户同学提供的日志文件,其中不乏几百M一G的也都有,毕竟压测一晚上产生的日志量还是很可观的,xDxD,因此不可避免的需要对日志进行分割,通常定位问题需要针 ...

  8. awk 命令使用

    awk命令就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理.例如: awk -F '/' '{print $NF}' tmp/file.txt 该命令用于读取file. ...

  9. spring后台重定向方式

    1.直接返回值中加重定向:"redirect:要访问的网址"; public String updateOrAddProject() { return "redirect ...

  10. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(协程池版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 from gevent import monkey monkey.patch_all() from gevent.pool i ...