题目描述

给你一棵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. C++输入输出常用格式(cin,cout,stringstream)

    输入格式 1.cin>>a; 最基本的格式,适用于各种类型.会过滤掉不可见字符例如空格,TAB,回车等 2.cin>>noskipws>>ch[i]; 使用了 no ...

  2. 安卓APP开发简单实例 结对编程心得

    开始说起搞APP开发,自己和小伙伴的编程水平真的很低,无从下手,只有在网上找点案列,学习着怎样开发,结对编程还是面临着许多问题的,大家的水平有所差异和编程风格不同,我们用eclipse做了一个仿微信登 ...

  3. SSM整合activiti框架

    一:WorkFlow简介 1:什么是工作流工作流(Workflow),指“业务过程的部分或整体在计算机应用环境下的自动化”.是对工作流程及其各操作步骤之间业务规则的抽象.概括描述.在计算机中,工作流属 ...

  4. log4j日志不输出的问题

    今天服务器上报错,想先去看一下日志进行排查,结果发现日志很久都没有输出过了.从上午排查到下午,刚刚解决,因此记录一下,但现在也只是知其然,并不知其所以然,所以如果大家有什么想法请在下方评论. 先说一下 ...

  5. jquery的api以及用法总结-选择器

    jQuery API及用法总结 选择器 基本选择器 * 通用选择器 .class 类选择器,一个元素可以有多个类(chrome使用原生js函数getElementByClassName()实现) 利用 ...

  6. 工厂模式(整理自李建忠<C++设计模式>视频)

    整理自李建忠<C++设计模式>视频 一.导入:"对象创建"模式和工厂模式 工厂模式只是该模式下的一种. 二.举例说明 有这样一个场景:需要在MainForm中设计一个按 ...

  7. 基于RHEL8/CentOS8的网络基础配置

    在rhel7上,同时支持network.service和NetworkManager.service(简称NM).默认情况下,这2个服务都有开启,但许多人都会将NM禁用掉.而在rhel8上,已废弃ne ...

  8. hbase、pig、hive配置与应用

    ------------------HBASE---------- [root@iClient~]#sudo yum install hbase #iClient安装Hbase客户端 [root@cM ...

  9. Python-基于向量机SVM的文本分类

    项目代码见 Github: 1.算法介绍 2.代码所用数据 详情参见http://qwone.com/~jason/20Newsgroups/ 文件结构 ├─doc_classification.py ...

  10. codeforce-1201-C题解

    题目:给你一个包含n个整数的数组A(n为奇数),对A做k次以下操作: 对数组排序使数组以非递减顺序排列. 选取数组的中位数,然后加一 最终使得数组的中位数最大. 输入:第一行输入两个数字 n 和 k ...