题面

题解

感觉和\(CDQ\)分治一样套路啊

首先,构建出点分树

对于每一层分治重心,求出它到子树中任意点的距离

然后\(two-pointers\)计算满足小于等于\(K\)的点对数目,加入答案

但是可能会算重,那么就减去子树内两两点之间的贡献即可。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x)); namespace IO
{
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
inline char getchar() { if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin); return *is++; }
} inline int read()
{
int data = 0, w = 1;
char ch = IO::getchar();
while(ch != '-' && (ch < '0' || ch > '9')) ch = IO::getchar();
if(ch == '-') w = -1, ch = IO::getchar();
while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::getchar();
return data*w;
} const int maxn(40010);
struct edge { int next, to, dis; } e[maxn << 1];
int head[maxn], e_num, n, Size, Max, root, stk[maxn], dep[maxn], top, size[maxn], K, ans, vis[maxn];
inline void add_edge(int from, int to, int dis) { e[++e_num] = (edge) {head[from], to, dis}; head[from] = e_num; } void GetRoot(int x, int fa)
{
size[x] = 1; int max = 0;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(to == fa || vis[to]) continue;
GetRoot(to, x); size[x] += size[to]; max = std::max(max, size[to]);
}
max = std::max(max, Size - size[x]);
if(max < Max) Max = max, root = x;
} void GetDep(int x, int fa)
{
stk[++top] = dep[x];
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(to == fa || vis[to]) continue;
dep[to] = dep[x] + e[i].dis; GetDep(to, x);
}
} int Calc(int x, int pre)
{
top = 0; dep[x] = pre; GetDep(x, 0); std::sort(stk + 1, stk + top + 1);
int l = 1, r = top, sum = 0;
while(l < r) { if(stk[l] + stk[r] <= K) sum += r - l, ++l; else --r; }
return sum;
} void Solve(int x)
{
ans += Calc(x, 0); vis[x] = 1;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to; if(vis[to]) continue;
ans -= Calc(to, e[i].dis); Size = Max = size[to];
GetRoot(to, x); Solve(root);
}
} int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif Max = Size = n = read();
for(RG int i = 1, a, b, c; i < n; i++)
a = read(), b = read(), c = read(), add_edge(a, b, c), add_edge(b, a, c);
K = read(); GetRoot(1, 0); Solve(root); printf("%d\n", ans);
return 0;
}

【洛谷P4178】Tree的更多相关文章

  1. 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)

    推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...

  2. POJ1471 Tree/洛谷P4178 Tree

    Tree P4178 Tree 点分治板子. 点分治就是直接找树的重心进行暴力计算,每次树的深度不会超过子树深度的\(\frac{1}{2}\),计算完就消除影响,找下一个重心. 所以伪代码: voi ...

  3. 洛谷P4178 Tree (点分治)

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

  4. 洛谷 P4178 Tree —— 点分治

    题目:https://www.luogu.org/problemnew/show/P4178 这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美: 没有写 sor ...

  5. 洛谷P4178 Tree (算竞进阶习题)

    点分治 还是一道点分治,和前面那道题不同的是求所有距离小于等于k的点对. 如果只是等于k,我们可以把重心的每个子树分开处理,统计之后再合并,这样可以避免答案重复(也就是再同一个子树中出现路径之和为k的 ...

  6. 2018.07.20 洛谷P4178 Tree(点分治)

    传送门 又一道点分治. 直接维护子树内到根的所有路径长度,然后排序+双指针统计答案. 代码如下: #include<bits/stdc++.h> #define N 40005 using ...

  7. [洛谷P4178]Tree

    题目大意:给一棵树,问有多少条路径长度小于等于$k$ 题解:点分治 卡点:无 C++ Code: #include <cstdio> #include <algorithm> ...

  8. 洛谷 P4178 Tree

    #include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #inclu ...

  9. [洛谷P4178] Tree (点分治模板)

    题目略了吧,就是一棵树上有多少个点对之间的距离 \(\leq k\) \(n \leq 40000\) 算法 首先有一个 \(O(n^2)\) 的做法,枚举每一个点为起点,\(dfs\) 一遍可知其它 ...

  10. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

随机推荐

  1. flume MemoryChannel 源代码解析

    1.先分析三个LinkedBlockingDeque<Event>类型的takeList,putList,queue putList:  存放的是来自source生产的数据,通过调用doP ...

  2. POJ2074 Line of Sight

    嘟嘟嘟 题意:用一条水平线段表示以栋房子:\((x_0, y_0)(x_0', y_0)\).然后有一条低于房子的水平线段\(l_0\),代表你可以到的位置.接下来输入一个数\(n\),一下\(n\) ...

  3. 1、Android-活动(下)

    1.4.活动的生命周期 对于活动来说生命周期的理解时非常重要的 当对其生命周期有了足够的了解,可以很好的写出程序 1.4.1.返回栈 Android中的活动是可以层叠的 没启动一个新的活动,就会立即覆 ...

  4. Kali-linux使用SET实施攻击

    前面介绍了社会工程学工具包(SET)的简单使用.为了能帮助用户更容易的理解社会工程学的强大功能.本节将介绍使用社会工程学工具包实施各种攻击. 7.4.1 针对性钓鱼攻击向量 针对性钓鱼攻击向量通过构造 ...

  5. HDU 1004 Let the Balloon Rise(map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  6. CSP 试题编号201803-1 Java实现

    import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input ...

  7. 基于Python在MacOS上安装robotframework-ride

    基于Python在MacOS上安装robotframework-ride https://www.jb51.net/article/153665.htm https://www.jianshu.com ...

  8. 【js】Redux基本原理和使用

    Redux不是说任何的应用都要用到它,如果遇到了react解决不了得问题,可以考虑使用它. 例如: 用户的使用方式复杂不同身份的用户有不同的使用方式(比如普通用户和管理员)多个用户之间可以协作与服务器 ...

  9. nodejs中npm以及yarn常用指令

    1.npm下载相关 1.npm install/i vue //下载vue的包 2.npm i vue --save-dev / -D //下载vue的包,并添加到开发依赖中 3.npm i //下载 ...

  10. js数组定义和方法 (包含ES5新增数组方法)

    数组Array 1. 数组定义 一系列数据的集合成为数组.数组的元素可以为任何类型的数据(包括数组,函数等),每个元素之间用逗号隔开,数组格式:[1,2,3]. 2. 数组创建方式 (1) 字面量方法 ...