树分治之点分治入门

所谓点分治,就是对于树针对点的分治处理

首先找出重心以保证时间复杂度

然后递归处理所有子树

对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况

  1. 路径过当前根
  2. 路径在子树中(递归处理)

那么关键就是如何计算第一种情况

设d[i]表示点i到当前根rt的距离,可以将d数组排序后线性复杂度求

然而此时会有些点对是在同一棵子树中,这些情况要减去

注意每次递归都要找一次重心以保证效率

这样复杂度就是O(nlog2n)

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#define Inf 0x7fffffff
#define N 10010
using namespace std; struct info{int to,nex,w;}e[N*2];
int n,k,tot,head[N],d[N],rt,Ans,sum,f[N],sz[N],dep[N];
bool vis[N]; void Link(int u,int v,int w){
e[++tot].nex=head[u];head[u]=tot;e[tot].to=v;e[tot].w=w;
} inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void getrt(int u,int fa){
sz[u]=1,f[u]=0;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(v==fa||vis[v]) continue;
getrt(v,u);
sz[u]+=sz[v];
f[u]=max(f[u],sz[v]);
}
f[u]=max(f[u],sum-sz[u]);
if(f[rt]>f[u]) rt=u;
} void Init(){
tot=0,rt=0,Ans=0;
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
Link(u,v,w),Link(v,u,w);
}
sum=n,f[0]=Inf;
getrt(1,0);
} void getdep(int u,int fa){
dep[++dep[0]]=d[u];
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(v==fa||vis[v]) continue;
d[v]=d[u]+e[i].w;
getdep(v,u);
}
} int cal(int u,int cur){
d[u]=cur,dep[0]=0;
getdep(u,0);
sort(dep+1,dep+dep[0]+1);
int t=0;
for(int l=1,r=dep[0];l<r;)
if(dep[l]+dep[r]<=k) t+=r-l,++l;
else --r;
return t;
} void solve(int u){
Ans+=cal(u,0);
vis[u]=1;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(vis[v]) continue;
Ans-=cal(v,e[i].w);
sum=sz[v];
getrt(v,rt=0);
solve(rt);
}
} int main(){
while(~scanf("%d%d",&n,&k)&&n){
Init();
solve(rt);
printf("%d\n",Ans);
}
}

[POJ1741]Tree(点分治)的更多相关文章

  1. [poj1741]Tree(点分治+容斥原理)

    题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...

  2. [bzoj1468][poj1741]Tree[点分治]

    可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...

  3. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  4. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  5. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  6. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  7. POJ1741 tree 【点分治】

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25286   Accepted: 8421 Description ...

  8. POJ1741 Tree(树的点分治基础题)

    Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...

  9. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

随机推荐

  1. 015.1 Lock接口

    内容:Lock接口使用步骤,同步生产大白兔奶糖的例子 同步代码块的锁是隐式的,显式容易让我们理解.所以我们使用这个显式的方法,方便理解代码.######实现同步步骤:1.获取锁:lock()2.同步代 ...

  2. 【转】ios 抓取 tcp/udp 包

    原文: http://useyourloaf.com/blog/2012/02/07/remote-packet-capture-for-ios-devices.html Remote Packet ...

  3. web自动化_浏览器驱动chromedriver安装方法(适用RF框架/Selenium/Appium)

    在进行UI自动化时,打开浏览器是第一步,这就必须要安装浏览器的驱动,chrome浏览器需要安装chromedriver,下载地址:http://chromedriver.storage.googlea ...

  4. IOS 缓存思路

    为了提高程序的响应速度,可以考虑使用缓存(内存缓存\硬盘缓存) 第一次请求数据时内存缓存中没有数据硬盘缓存中没有数据 当服务器返回数据时,需要做以下步骤 使用服务器的数据(比如解析.显示) 将服务器的 ...

  5. Asp.net & Aspose.cells 导入

    Workbook workBook = new Workbook(this.fuFile.FileContent); Aspose.Cells.Worksheet sheet = workBook.W ...

  6. BZOJ 1084 最大子矩阵 dp

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1084 题目大意: 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分 ...

  7. 【[IOI2005]Riv 河流】

    趁魏佬去英语演讲了,赶快%%%%%%%%%%%%%%魏佬 基本上是照着魏佬的代码写的 这其实还是一个树上背包 我们用\(dp[i][j][k]\)表示在以\(i\)为根的子树里,我们修建\(k\)个伐 ...

  8. python沙箱逃逸的几道题

    第一道 from __future__ import print_function print("Welcome to my Python sandbox! Enter commands b ...

  9. 【MySQL-123】MySQL8.0.12 安装于Win10

    参考blog:MySQL8.0.12 安装及配置 [坑一]输入net start mysql时,MYSQL服务无法启动. 问题:第三步my.ini文件编码错误. 解决方案:https://blog.c ...

  10. 3、Spring Cloud - Eureka(高可用Eureka Server集群)

    在实际的项目中,可能有几十个或者几百个的微服务实例,这时 Eureka Server 承担了非 常高的负载.由于 Eureka Server 在微服务架构中有着举足重轻的作用,所以需要对 Eureka ...