题目描述

给你一棵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. Jmeter安装图文及入门教程

    一.JMeter介绍 JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域.它可以用于测试静态和动态资源,例如 ...

  2. thinkphp6 常用方法文档

    请求变量 use think\facade\Request; Request::param('name'); Request::param();全部请求变量 返回数组 Request::param([ ...

  3. 松软科技课堂:Winform之TextBox

    松软科技文(www.sysoft.net.cn): 文本框的几种模式:Multiline(多行).PasswordChar(密码)将文本框的PasswordChar设为*就是密码框效果,将MultiL ...

  4. 让Samba支持Windows10的自动发现

    Windows10如果开了SMB 1.0支持,就非常不安全,不开就搜索不到samba的NETBIOS. 在安装配置好samba,并且确认windows可以通过netbios名访问后. 可以使用http ...

  5. Linux之vim、压缩与解压缩

    终有一种方法适合你!

  6. 素数路径Prime Path POJ-3126 素数,BFS

    题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...

  7. 学 Java 网络爬虫,需要哪些基础知识?

    说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...

  8. forEach标签

    1.forEach标签的简单使用: (1)未设置步长属性时,默认步长为1: <c:forEach "> <c:out value="${number}" ...

  9. office2019激活

    这个是在网上偶然看见的一个激活方式,分享一下. 复制如下代码保存后修改文件后缀名为".bat",请注意有一个点,然后保存以管理员身份运行即可: @echo off(cd /d &q ...

  10. thinkphp5 模型表关联

    student 表 外键 grade_idgrade 表主键 id在 模型中student表关联方法public function Grade(){ return $this->hasOne(' ...