洛谷P4178 Tree (点分治)
题目描述
给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K
输入输出格式
输入格式:
N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k
输出格式:
一行,有多少对点之间的距离小于等于k
输入输出样例
7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
5 题解:点分裸题,考虑分治中的暴力,将所有的重心子树中的点到中心的距离排序,对于一组l-r之间如果d[l]+d[r]<=k,显然d[l]+d[i](i<r)时都满足d[l]+d[i]<=k,可以统计答案,类似尺取的思想。
总复杂度是O(nlognlogn) 代码如下:
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mp make_pair
#define pii pair<int,int>
using namespace std; vector<pii> g[];
int n,k,f[],vis[],size[],di[],cnt,ans; void get_size(int now,int fa)
{
size[now]=;
f[now]=fa;
for(int i=;i<g[now].size();i++)
{
if(vis[g[now][i].first]||g[now][i].first==fa) continue;
get_size(g[now][i].first,now);
size[now]+=size[g[now][i].first];
}
} int get_zx(int now,int fa)
{
if(size[now]==) return now;
int son,maxson=-;
for(int i=;i<g[now].size();i++)
{
if(vis[g[now][i].first]||g[now][i].first==fa) continue;
if(maxson<size[g[now][i].first])
{
maxson=size[g[now][i].first];
son=g[now][i].first;
}
}
int zx=get_zx(son,now);
while(size[zx]<(size[now]-size[zx])*) zx=f[zx];
return zx;
} void get(int now,int fa,int dis)
{
di[++cnt]=dis;
for(int i=;i<g[now].size();i++)
{
if(vis[g[now][i].first]||g[now][i].first==fa) continue;
get(g[now][i].first,now,dis+g[now][i].second);
}
} int calc(int now,int dis)
{
cnt=;
int tmp=;
get(now,,dis);
sort(di+,di+cnt+);
int l=,r=cnt;
while(l<=r)
{
if(di[l]+di[r]<=k)
{
tmp+=r-l;
l++;
}
else r--;
}
return tmp;
} void solve(int now)
{
ans+=calc(now,);
vis[now]=;
for(int i=;i<g[now].size();i++)
{
if(vis[g[now][i].first]) continue;
ans-=calc(g[now][i].first,g[now][i].second);
get_size(g[now][i].first,);
int zx=get_zx(g[now][i].first,);
solve(zx);
}
} int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
int from,to,cost;
scanf("%d%d%d",&from,&to,&cost);
g[from].push_back(mp(to,cost));
g[to].push_back(mp(from,cost));
}
scanf("%d",&k);
solve();
printf("%d\n",ans);
}
洛谷P4178 Tree (点分治)的更多相关文章
- 洛谷 P4178 Tree —— 点分治
题目:https://www.luogu.org/problemnew/show/P4178 这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美: 没有写 sor ...
- [洛谷P4178] Tree (点分治模板)
题目略了吧,就是一棵树上有多少个点对之间的距离 \(\leq k\) \(n \leq 40000\) 算法 首先有一个 \(O(n^2)\) 的做法,枚举每一个点为起点,\(dfs\) 一遍可知其它 ...
- POJ1471 Tree/洛谷P4178 Tree
Tree P4178 Tree 点分治板子. 点分治就是直接找树的重心进行暴力计算,每次树的深度不会超过子树深度的\(\frac{1}{2}\),计算完就消除影响,找下一个重心. 所以伪代码: voi ...
- 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)
推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...
- 2018.07.20 洛谷P4178 Tree(点分治)
传送门 又一道点分治. 直接维护子树内到根的所有路径长度,然后排序+双指针统计答案. 代码如下: #include<bits/stdc++.h> #define N 40005 using ...
- 洛谷 4178 Tree——点分治
题目:https://www.luogu.org/problemnew/show/P4178 点分治.如果把每次的 dis 和 K-dis 都离散化,用树状数组找,是O(n*logn*logn),会T ...
- 洛谷P4178 Tree (算竞进阶习题)
点分治 还是一道点分治,和前面那道题不同的是求所有距离小于等于k的点对. 如果只是等于k,我们可以把重心的每个子树分开处理,统计之后再合并,这样可以避免答案重复(也就是再同一个子树中出现路径之和为k的 ...
- [洛谷P4178]Tree
题目大意:给一棵树,问有多少条路径长度小于等于$k$ 题解:点分治 卡点:无 C++ Code: #include <cstdio> #include <algorithm> ...
- 洛谷 P4178 Tree
#include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #inclu ...
随机推荐
- Tow types of EF's default connection
When you create a new project that makes use of Entity Framework 5, you'll notice how it uses LocalD ...
- [Python] Request module
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
- excel之工作表工作簿保护暴力撤销
excal之工作表工作簿保护暴力撤销 excel可以在审阅中设置工作表.工作簿的密码保护,但是当密码忘记或一些特殊情况下需要进行操作. 1.工作簿保护撤销 步骤一:将需要破解的excal文件后缀名改为 ...
- GNU/Linux LVM 原理图释
逻辑卷管理器(英语:Logical Volume Manager,缩写为LVM),又译为逻辑卷宗管理器.逻辑扇区管理器.逻辑磁盘管理器,是Linux核心所提供的逻辑卷管理(Logical volume ...
- docker redis4.0 集群(cluster)搭建
前言 redis集群对于很多人来说非常熟悉,在前些日子,我也有一位大兄弟也发布过一篇关于在阿里云(centOS7)上搭建redis 集群的文章,虽然集群搭建的文章在网上很多,我比较喜欢这篇文章的地方是 ...
- 解决Tomcat 一闪而过的问题
启动tomcat时cmd窗口一闪而过解决方法. 问题现象: 在实际开发中一般都是eclipse+tomcat(也许还会用到tomcat的插件),我们只需要在eclipse中单击servers上的按钮就 ...
- Python常见函数用法
1. shape()函数 在numpy模块 输入参数:类似数组(比如列表,元组)等,或是数组 返回:一个整型数字的元组,元组中的每个元素表示相应的数组每一维的长度 # shape[0]返回对象的行数, ...
- C# Common Keyword II
[C# Common Keyword II] 1.as 运算符用于在兼容的引用类型之间执行某些类型的转换. class csrefKeywordsOperators { class Base { pu ...
- java基础强化——深入理解java注解(附简单ORM功能实现)
目录 1.什么是注解 2. 注解的结构以及如何在运行时读取注解 2.1 注解的组成 2.2 注解的类层级结构 2.3 如何在运行时获得注解信息 3.几种元注解介绍 3.1 @Retention 3.2 ...
- 82. Remove Duplicates from Sorted List II (List)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...