题面

题解

感觉和\(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. 为什么 Category 不能增加成员变量-nonfragile

    三.既然是 non-fragile ivars,为什么 Category 不能增加成员变量?     看过一些资料,理由并不是很让人信服.我觉得并不是做不到,只是现在没有做,现在不支持.我在 Opti ...

  2. 在Windows 10中更改网络连接优先级

    查看接口列表 (也可使用 如下) 选择网络连接,然后单击右侧的箭头以更改网络连接优先级.  可以参考之前的部分 链接在此 更改单个wi-fi连接顺序可以使用如下

  3. 浅谈Fluent Ribbon 中的SplitButton

    Fluent Ribbon Control Suite 就不做介绍了,网上的例子比较多,类似Office2007及以后版本的图形界面(菜单栏).官网地址:https://github.com/flue ...

  4. f-stack中nginx配置后make出现error: ignoring return value of ‘ftruncate’

    问题 Nginx 配置后 make 出现error: src/os/unix/ngx_process_cycle.c: In function 'ngx_start_worker_processes' ...

  5. sql-case when 条件1 then 取值1 when 条件2 then 取值2 else 取值3 end

    遇到 XXX情况 就 XXX 遇不到就 XXX 结束case when …… then …… else …… end 例如一个3条件取值的字段: case when 条件1 then 取值1 when ...

  6. eclipse的svn插件

    SVN插件下载地址及更新地址,你根据需要选择你需要的版本.现在最新是1.8.xLinks for 1.8.x Release:Eclipse update site URL: http://subcl ...

  7. duplicate symbol _OBJC_IVAR

    duplicate symbol _OBJC_IVAR - Kingdev - 博客园 代码合并问题 git合并提示冲突文件为project.pbxproj,先去掉所有冲突提示<<< ...

  8. File、Paths和Files类的使用详解

    Paths:通过get()方法返回一个Path对象,Path用于表示文件路径和文件. Files:提供了大量处理文件的方法,例如文件复制.读取.写入,获取文件属性.快捷遍历文件目录等..... Fil ...

  9. js中的冒泡排序

    <!-- 冒泡排序:把一组数列按照一定的顺序进行排列,从大到小,或者从小到大 -->          // 控制循环的轮数          arr = [3,14,55,2,1,4,5 ...

  10. CentOS 7 Minimal 安装JDK 1.8

    真好最近比较闲,打算在linux 的CentOS 7 Minimal版本试着搭建hadoop环境学习学习,当然第一步就是在CentOS 7 Minimal 安装JDK 1.8环境.其实老早就打算了解一 ...