洛谷P4178 Tree (点分治)

时间:2023-01-13 14:41:18

题目描述

给你一棵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

题解:点分裸题,考虑分治中的暴力,将所有的重心子树中的点到中心的距离排序,对于一组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 (点分治)的更多相关文章

  1. 洛谷 P4178 Tree —— 点分治

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

  2. &lbrack;洛谷P4178&rsqb; Tree &lpar;点分治模板&rpar;

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

  3. POJ1471 Tree&sol;洛谷P4178 Tree

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

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

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

  5. 2018&period;07&period;20 洛谷P4178 Tree(点分治)

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

  6. 洛谷 4178 Tree——点分治

    题目:https://www.luogu.org/problemnew/show/P4178 点分治.如果把每次的 dis 和 K-dis 都离散化,用树状数组找,是O(n*logn*logn),会T ...

  7. 洛谷P4178 Tree &lpar;算竞进阶习题&rpar;

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

  8. &lbrack;洛谷P4178&rsqb;Tree

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

  9. 洛谷 P4178 Tree

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

随机推荐

  1. 『&period;NET Core CLI工具文档』(十四)dotnet-install 脚本参考

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-install scripts reference 翻译:dotnet-install 脚本参考 名称 d ...

  2. 解释器模式(Interpreter Pattern)

    定义:给定一种语言,定义他的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中句子. 抽象解释器:声明一个所有具体表达式都要实现的抽象接口(或者抽象类),接口中主要是一个interpre ...

  3. 苹果IPSW文件提取软件

    ipsw文件 提取系统文件 方法总结 由于修改运营商文件造成我的有锁4S无法使用移动卡了,在网上苦寻一番还是没有结果,最后萌生了从固件中提取文件的想法,于是便开始在网上搜集资料,最后文件终于提取成功并 ...

  4. bzoj2054 疯狂的馒头

    bzoj上现在找不到这题,所以目前只是过了样例,没有测 2054: 疯狂的馒头 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 715  Solved: ...

  5. sqlserver2008安装教程

    这几天因为需要,一直想安装sql server 2008来作为Web后台的数据库进行些实验,但总是没有时间,今天终于有时间了,便安装了SQL Server 2008,以下是我的安装的步骤,希望对于有需 ...

  6. sharepoint2010无法创建网站集

    出现以上错误,查看IIS中有关Sharepoint的网站中的“身份验证”中ASP.Net模拟是否为禁用,如果为禁用,请启用即可.

  7. python 学习源码练习(1)

    #编译方式,python3 文件名 #!/usr/bin/python3#print('hello world') mystring = 'hello world'print (mystring) # ...

  8. zookeeper的原理,5分钟了解zookeeper

    一 .Zookeeper功能简介 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/ ...

  9. Spring事务的5种隔离级别和7种传播性

    隔离级别 isolation,5 种: ISOLATION_DEFAULT,ISOLATION_READ_UNCOMMITTED,ISOLATION_READ_COMMITTED,ISOLATION_ ...

  10. 使用Dockerfile制作JDK&plus;tomcat镜像

    1.准备好jdk和tomcatapache-tomcat-8.5.32.tar.gzjdk-8u181-linux-x64.tar.gz 注意:a.jdk和tomcat记得从官网下载,否则制作出来的镜 ...