[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 ...
随机推荐
- SharePoint2007深入浅出——使用jQuery UI
jQuery1.6.4.js + jQuery UI 1.8.16.js 只有这个版本在IE8下的兼容性视图+Quirks(文本模式),dialog可以正常显示.
- python 脚本运行时报错: AttributeError: 'module' object has no attribute ***
最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...
- 移动端不同分辨率适配--使用flexible
根据阿里手淘团队发布的可伸缩布局方案amfe-flexible,git地址:https://github.com/amfe/lib-flexible. 使用方法: lib-flexible库的使用方法 ...
- python连接数据库并插入数据
1.Python创建数据库 import MySQLdb try: conn = MySQLdb.connect( host="127.0.0.1", port=3306, use ...
- Mac eclipse导入项目中文乱码问题解决
方法一 1.打开eclipse 偏好设置 2.General ——>Content Types——>Text——>Java SourceFile 3.将编码设置为GBK. 4.upd ...
- 【JavaScript】particle
这是js实现的粒子动画,有两种模式,分别是zoom和line,它们对应的效果不同,但是原理都相同,具体分析如下: 部分程序如下: var p = this; p.originParams = orig ...
- [Python 多线程] asyncio (十六)
asyncio 该模块是3.4版本加入的新功能. 先来看一个例子: def a(): for x in range(3): print('a.x', x) def b(): for x in 'abc ...
- ethereumjs/ethereumjs-block-3-tests
之前可以先了解一下另一个模块,看本博客的ethereumjs/ethereumjs-common部分内容 通过tests测试文件能够帮助更好了解API的使用 ethereumjs-block/test ...
- 安装Centos 7 错误解决
dracut-initqueue[624]:Warning: Could not boot. dracut-initqueue[624]:Warning: /dev/root does not exi ...
- HDU 1829 A Bug's Life (种类并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...