题面

题解

感觉和\(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. iOS UI(绘图)的几张原理图

    Core Animation是对OpenGL ES的Objective-C封装,具有与OpenGL ES几乎等价的高性能,却隐藏了OpenGL ES的复杂性. https://www.cnblogs. ...

  2. 【[HEOI2016/TJOI2016]字符串】

    码农题啊 上来先无脑一个\(SA\)的板子,求出\(SA\)和\(het\)数组 我们只需要从\(sa[i]\in[a,b]\)的所有\(i\)中找到一个\(i\)使得\(sa[i]\)和\(rk[c ...

  3. [Python 多线程] 详解daemon属性值None,False,True的区别 (五)

    本文以多个例子介绍Python多线程中daemon属性值的区别. 回顾: 前面的文章简单介绍了在现代操作系统中,每一个进程都认为自己独占所有的计算机资源. 或者说线程就是独立的王国,进程间是相对独立的 ...

  4. 跟我一起学Linux-线程创建,类似FreeRTOS创建任务

    1.参考学习大神网址:http://blog.csdn.net/ithomer/article/details/6063067 #include<stdio.h> #include< ...

  5. Linux 防止rm -rf 误删Shell脚本

    #!/bin/bash #:set ff=unix #:set nobomb #-*- coding:utf-8 -*- ####################################### ...

  6. Notes 20180509 : Java基本数据类型

    计算机就是个机器,这个机器由CPU.内存.硬盘和输入输出设备组成.计算机上运行着操作系统,操作系统提供对外的接口供各厂商和开发语言,开发运行在其上的驱动和应用程序. 操作系统将时间分成细小的时间碎片, ...

  7. iOS项目启动及启动时间优化

    app的启动入口Main函数: int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc ...

  8. ImageMagick使用小结

    #查看是否安装imagemagick sudo apt list | grep imagemagick #安装imagemagick sudo apt-get install imagemagick ...

  9. activemq的高级特性:消息的可靠性

    高级特性之消息的可靠性 可靠性分为:生产者.消费者.生产者必须让mq收到消息,消费者必须能够接收到消息并且消费成功,这就是消息的可靠性. 1:生产者可靠性 Session session = conn ...

  10. JavaScript中的this详解(彻底弄懂js中的this用法)!

    要想学好js,那么其中那些特别令人混淆迷惑的知识点,就一定要弄清楚.this关键字就是其中让初学者比较迷惑的知识点之一,不过灵活运用this可以提升代码的性能和复用性,那么今天我就和大家一起来了解th ...