【BZOJ】1468: Tree(点分治)
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
Input
Output
Sample Input
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
HINT
Source
【BZOJ】1468: Tree(点分治)的更多相关文章
- bzoj 1468 Tree(点分治模板)
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1527 Solved: 818[Submit][Status][Discuss] ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- BZOJ 1468: Tree
Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...
- 【刷题】BZOJ 1468 Tree
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
- [BZOJ 3456]城市规划(cdq分治+FFT)
[BZOJ 3456]城市规划(cdq分治+FFT) 题面 求有标号n个点无向连通图数目. 分析 设\(f(i)\)表示\(i\)个点组成的无向连通图数量,\(g(i)\)表示\(i\)个点的图的数量 ...
- [BZOJ 2989]数列(CDQ 分治+曼哈顿距离与切比雪夫距离的转化)
[BZOJ 2989]数列(CDQ 分治) 题面 给定一个长度为n的正整数数列a[i]. 定义2个位置的graze值为两者位置差与数值差的和,即graze(x,y)=|x-y|+|a[x]-a[y]| ...
随机推荐
- 异常详细信息: System.Data.SqlClient.SqlException:用户 'IIS APPPOOL\DefaultAppPool' 登录失败解决办法
1.安全性---登录名---新建登录名 2.常规----搜索 3.添加SERVICE用户-- 4.服务器角色---勾上sysadmin: IIS中: 应用程序池---对应的程序池上右键---高级设置 ...
- PHP表单验证
<!DOCTYPE html> <html> <head> <title>Test Code</title> </head> & ...
- 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
冬令营被平衡树坑了之后,打算苦练一番数据结构(QAQ). 先是打了一下想学好久的替罪羊树. 替罪羊树实现方法很简单,就是在不满足平衡条件的时候暴力重构子树. 调试小结: 1.删除操作分两类情况:如果某 ...
- PeopleEditor允许客户端输入的同时验证输入的内容
如何判断PeopleEditor的值为空 在sharepoint开发中,我们经常会用到PeopleEditor这一控件,最近我在写程序的时候用到了,开始的时候不知道怎么用,后来问题解决啦,现在写出 ...
- Java数据类型中String、Integer、int相互间的转换
1.Integer转换成int的方法 Integer i; int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Int ...
- block引发的陷阱
block在项目的开发中使用时非常频繁的,苹果官方也极力推荐使用block.其实,究其本质,block就是指向结构体的指针(可利用运行时机制查看底层生成的c代码).然而在使用block时会存在很多陷阱 ...
- 【USACO】milk3
倒牛奶的问题, 开始看感觉跟倒水的问题很像, 想直接找规律, 写个类似于循环取余的代码. 但后来发现不行,因为这道题有三个桶,水量也是有限制的.只好用模拟的方法把所有的情况都试一遍. 建一个state ...
- tuple元组(C++11及以后,如C++14)
类tuple与array最本质的区别当数tuple元组元素类型可以不一样,而统一数组array的元素类型必须一样. 本文主要举例: tuple_size Example 123456789101112 ...
- Android VLC播放器二次开发1——程序结构分析
最近因为一个新项目需要一个多媒体播放器,所以需要做个视频.音频.图片方面的播放器.也查阅了不少这方面的资料,如果要从头做一个播放器工作量太大了,而且难度也很大.所以最后选择了VLC作为基础,进行二次开 ...
- hdu 4267 多维树状数组
题意:有一个序列 "1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i ...