http://www.lydsy.com/JudgeOnline/problem.php?id=1468

分治真是一门高大上的东西。。。

好神。。。

树分治最好资料是:qzc的《分治算法在树的路径问题中的应用》

我来说说自己的理解:

点分=找重心+分治

找重心尤为重要,因为这关系到时间复杂度。

对于递归式

$$T(n)=aT(n/b)+O(D(n))$$

这类递归式,如果能保证每一层都是$O(D(n))$,那么时间复杂度会大大减小。(详见算导第三章和第四章)

对于一棵树,如果我们在找到重心后,用线性做法处理完当前层,那么就可以log级别处理完整棵树,比如:

递归式

$$T(n)=aT(n/a)+O(n)$$

就是非常一般的点分的分治,在这里复杂度为$O(nlog_{a}n)$,具体证明看算导。。(很简单的。。。。。

然而在树分治中,最多递归log次(因为是找了重心),因此复杂度降到$O(nlgn)$

因此如果我们能线性时间内处理好每一层,问题就能在nlgn的时间内得以解决。。。。。

本题裸的路径询问。。。。。。。。。。。。。。。。。。。。。。。。。。。对于此类问题,考虑经过每个点的路径。。。。

预处理当前根所有子树的信息,然后加上根然后合并就能得到一条经过根的路径!然后就行了。。。。。随便搞搞就能线性了。。

本题只要将当前根所有子树节点到根的距离全部跑出来,排序后因为单调维护一下即可。。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=40005, oo=~0u>>1;
int ihead[N], cnt, K;
struct dat { int next, to, w; }e[N<<1];
void add(int u, int v, int w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].w=w;
} int dep[N], d[N], cdep, ans, mn;
int root, sz[N], vis[N];
void getroot(int x, int fa, int sum) {
sz[x]=1; int y, mx=0;
rdm(x, i) if(!vis[y=e[i].to] && e[i].to!=fa) {
getroot(y, x, sum);
sz[x]+=sz[y];
mx=max(mx, sz[y]);
}
mx=max(mx, sum-mx);
if(mx<mn) mn=mx, root=x;
}
void getdep(int x, int fa) {
dep[++cdep]=d[x]; int y; //printf("x:%d\tfa:%d\tdep:%d\n", x, fa, dep[x]);
rdm(x, i) if(!vis[y=e[i].to] && e[i].to!=fa) {
d[y]=d[x]+e[i].w;
getdep(y, x);
}
}
int cal(int x, int last=0) {
cdep=0; d[x]=last; //printf("root is:%d\n", x);
getdep(x, -1); //puts("==========================");
int ret=0, front=1, tail=cdep;
sort(dep+1, dep+1+cdep);
while(front<tail) {
while(front<tail && dep[tail]+dep[front]>K) --tail;
ret+=tail-front;
++front;
}
return ret;
}
void dfs(int x, int all) {
vis[x]=1; int y;
ans+=cal(x); //printf("root:%d\n", x);
rdm(x, i) if(!vis[y=e[i].to]) {
ans-=cal(y, e[i].w);
int s=sz[y]>sz[x]?all-sz[x]:sz[y];
root=0; mn=oo; getroot(y, x, s);
dfs(root, s);
}
} int main() {
int n=getint();
rep(i, n-1) { int u=getint(), v=getint(), w=getint(); add(u, v, w); }
read(K); mn=oo;
getroot((n+1)>>1, -1, n);
dfs(root, n);
printf("%d\n", ans);
return 0;
}

  


Description

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

Input

N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k

Output

一行,有多少对点之间的距离小于等于k

Sample Input

7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10

Sample Output

5

HINT

 

Source

【BZOJ】1468: Tree(点分治)的更多相关文章

  1. bzoj 1468 Tree(点分治模板)

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1527  Solved: 818[Submit][Status][Discuss] ...

  2. BZOJ.1468.Tree(点分治)

    BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...

  3. BZOJ 1468 Tree 【模板】树上点分治

    #include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...

  4. BZOJ 1468: Tree

    Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...

  5. 【刷题】BZOJ 1468 Tree

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  6. bzoj 1468 Tree 点分

    Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1972  Solved: 1101[Submit][Status][Discuss] Desc ...

  7. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  8. bzoj 2212 Tree Rotations

    bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...

  9. [BZOJ 3456]城市规划(cdq分治+FFT)

    [BZOJ 3456]城市规划(cdq分治+FFT) 题面 求有标号n个点无向连通图数目. 分析 设\(f(i)\)表示\(i\)个点组成的无向连通图数量,\(g(i)\)表示\(i\)个点的图的数量 ...

  10. [BZOJ 2989]数列(CDQ 分治+曼哈顿距离与切比雪夫距离的转化)

    [BZOJ 2989]数列(CDQ 分治) 题面 给定一个长度为n的正整数数列a[i]. 定义2个位置的graze值为两者位置差与数值差的和,即graze(x,y)=|x-y|+|a[x]-a[y]| ...

随机推荐

  1. 【Python】Django 时间字段 最佳实践

    . python datetime from datetime import datetime datetime.now() datetime.utcnow() from datetime impor ...

  2. 【架构】浅谈web网站架构演变过程

    浅谈web网站架构演变过程   前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变.   该系统具备的功能:   用户模块:用户注册和管理 商品模块:商品展示和管 ...

  3. SQL按照指定顺序对字段进行排序

    SqlServer按照指定顺序对字段进行排序 问题如下,在SqlServer有一个这样的SQL查询 SELECT *FROM ProductWHERE ID IN ( 12490, 12494, 12 ...

  4. 对 Linux 新手非常有用的 20 个命令

    参考:http://www.oschina.net/translate/useful-linux-commands-for-newbies 英文原文:http://www.tecmint.com/us ...

  5. bootbox显示中文的按钮

    $("selector").on('click',function(){ bootbox.confirm({ title : "请确认", buttons: { ...

  6. linux 学习一:安装jdk和tomcat

    使用secureCRT 一.首先安装centos的rzsz. 1.yum自动安装:(yum安装rzsz) yum install lrzsz 2.手动安装方法如下:(包有问题,还是采用第一种方式) 2 ...

  7. hdu2089

    基本的数位dp #include <cstdio> #include <cstring> using namespace std; #define D(x) x ; int n ...

  8. 搭建Maven工程的时候,做单元测试,报ClassNotFoundException

    搭建Maven工程的时候报错 问题原因是在spring.xml中配置的  classpath:config.properties  没有在工程中创建.

  9. discuz 学习

    一.Discuz首页“今日”“昨日”“欢迎新会员”等文字删除添加 搜索templeta/default/forum/discuz.htm   (使用非默认模版的请修改当前使用模版的discuz.htm ...

  10. struts1老古董配置

    <!--Struts1 struts-config.xml Demo --><?xml version="1.0" encoding="UTF-8&qu ...