[POJ1741]Tree(点分治)
树分治之点分治入门
所谓点分治,就是对于树针对点的分治处理
首先找出重心以保证时间复杂度
然后递归处理所有子树
对于这道题,对于点对(u,v)满足dis(u,v)<=k,分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(点分治)的更多相关文章
- [poj1741]Tree(点分治+容斥原理)
题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- [poj1741][tree] (树/点分治)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ1741 tree 【点分治】
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25286 Accepted: 8421 Description ...
- POJ1741 Tree(树的点分治基础题)
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- 部署Docker distribution仓库
环境准备: 下载docker yum文件 # wget -O /etc/yum.repos.d/aliyun.repo http://mirrors.aliyun.com/repo/Centos-7. ...
- Retrieving failed records after an SqlBulkCopy exception
Let me start by saying that the idea I used in this article is not originally mine, but since I have ...
- php中的static
静态成员是一种类变量,可以把它看成时属于整个类而不是属于类的某个实例.与一般的实例变量不同的是,静态成员只保留一个变量值,而这个变量值对所有的实例都是有效的,也就是说,所有的实例共享这个成员. $th ...
- 一个简单的php分页逻辑
php分页 <?php include 'backend/conn.php'; $html = '<ul>'; //输出的html $pageDataNum=3; //每页显示10行 ...
- 阿里云 CentOS 镜像和 EPEL 源
配置阿里云网络yum源 阿里云镜像源地址http://mirrors.aliyun.com/ 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.re ...
- jquery cookie插件
jquery-cookie下载地址:http://www.bootcdn.cn/jquery-cookie/ 使用方法: 1.引入jQuery.Cookie.js插件. <script src= ...
- unittest:2 执行多条用例,仅执行一次setUp和tearDown
对象方法setUp()和tearDown() 每个用例执行前后都会被调用.但是有另外一种场景:setUp之后执行完所有用例,最后调用一次tearDown.比如打开网页,多条用例分别验证网页上的元素正确 ...
- PhoneGap 介绍
一.PhoneGap 是什么 1.PhoneGap 是一个用基于 HTML,CSS 和 JavaScript 的,创建移动跨平台移动应用程序的快速开发框架. 2.它使开发者能够利用 iPhone,An ...
- iOS UI(绘图)的几张原理图
Core Animation是对OpenGL ES的Objective-C封装,具有与OpenGL ES几乎等价的高性能,却隐藏了OpenGL ES的复杂性. https://www.cnblogs. ...
- Autorelease 性能测试
__weak NSString *string_weak_ = nil; - (void)viewDidLoad { [super viewDidLoad]; // 场景 1 NSString *st ...