【BZOJ 1468】Tree 点分治
点分治$O(nlogn)$
坚持到月考结束后新校就剩下我一个OIer,其他人早已停课了,老师估计懒得为我一个人开机房门,让我跟班主任说了一声,今晚就回到了老校,开始了自己都没有想到会来的这么早的停课生活。
所以先水一道点分治
#include<cstdio>
#include<algorithm>
#define read(x) x=getint()
#define N 40003
#define max(a,b) (a)>(b)?(a):(b)
using namespace std;
inline int getint() {
char c; int fh=1, k=0;
for( ; c < '0' || c > '9'; c=getchar()) if ( c == '-') fh = -1;
for( ; c >= '0' && c <= '9'; c=getchar()) k = k * 10 + c - '0';
return k * fh;
}
struct node {
int nxt, to, w;
} E[N << 1];
bool vis[N];
int root, rtm = N, n, K, cnt = 0, sz[N], di[N], tb[N], tn, ans = 0, point[N];
inline void ins( int x, int y, int z) {cnt++; E[cnt].nxt = point[x]; E[cnt].to = y; E[cnt].w = z; point[x] = cnt;}
inline void fdrt( int x, int fa, int s) {
sz[x] = 1;
int ma=0;
for( int tmp = point[x]; tmp; tmp = E[tmp].nxt)
if ( !vis[E[tmp].to] && E[tmp].to != fa) {
fdrt( E[tmp].to, x, s);
sz[x] += sz[E[tmp].to];
ma = max( ma, sz[E[tmp].to]);
}
ma = max( ma, s - ma);
if ( ma < rtm) {
rtm = ma;
root = x;
}
}
inline void mktb( int x, int fa) {
tb[++tn] = di[x];
for( int tmp = point[x]; tmp; tmp = E[tmp].nxt)
if ( !vis[E[tmp].to] && E[tmp].to != fa) {
di[E[tmp].to] = di[x] + E[tmp].w;
mktb( E[tmp].to, x);
}
}
inline int work( int x, int la) {
di[x] = la;
tn = 0;
mktb( x, -1);
sort( tb + 1, tb + tn + 1);
int head = 1, tail = tn, an = 0;
while ( head < tail) {
while ( head < tail && tb[head] + tb[tail] > K)
--tail;
an += tail - head;
++head;
}
return an;
}
inline void dfs( int x, int s) {
vis[x] = 1;
ans += work( x, 0);
for( int tmp = point[x]; tmp; tmp = E[tmp].nxt)
if ( !vis[E[tmp].to]) {
ans -= work( E[tmp].to, E[tmp].w);
rtm = N;
int ss = sz[x] > sz[E[tmp].to] ? sz[E[tmp].to] : s - sz[x];
fdrt( E[tmp].to, x, ss);
dfs( root, ss);
}
}
int main() {
read(n);
int u, v, e;
for( int i = 1; i < n; ++i) {
read(u); read(v); read(e);
ins( u, v, e);
ins( v, u, e);
}
read(K);
fdrt( 1, -1, n);
dfs( root, n);
printf( "%d\n", ans);
return 0;
}
第一次交我忘了sort了,,,
【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]| ...
随机推荐
- AC日记——接龙游戏 codevs 1051
1051 接龙游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给出了N个单词,已经按长度排好了序.如果 ...
- 迄今最深入、最专业的Hololens评测结果,美国AR大咖艾迪·奥夫曼现身说法
http://blackx.baijia.baidu.com/article/530213 在空间记忆方面,HoloLens也有着自己独特的解决方案. 在支持Tango的设备中,将一个虚拟物体放置在某 ...
- Android:关于声明文件中android:process属性说明
笔者在学习Android Service组件的过程中碰到了一个问题,就是在Android应用的声明文件Manifest.xml中有时候会对相关的服务标签设置一个android:process=&quo ...
- ef操作类
基于Hi博客的类库 20160811 using Model; using System; using System.Collections.Generic; using System.Data.En ...
- js数组操作
用 js有很久了,但都没有深究过js的数组形式.偶尔用用也就是简单的string.split(char).这段时间做的一个项目,用到数组的地方很多, 自以为js高手的自己居然无从下手,一下狠心,我学! ...
- WIN7下强制分第四个主分区的方法
通过磁盘管理的界面方式, 第四个分区会被分成扩展分区, 建议通过命令行 打开命令行运行diskpart, list disk 会列出所有磁盘, 选择要操作的磁盘序号如1,select disk 1 如 ...
- Yeo 17-ROI parcellation
Reference Buckner R L, Krienen F M, Castellanos A, et al. The organization of the human cerebellum e ...
- git push ERROR: missing Change-Id in commit message footer
今天上传代码时候报告错误:$ git push origin HEAD:refs/for/branch*Counting objects: 7, done.Delta compression usin ...
- 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值
接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...
- 与Python Falling In Love_Python跨台阶(面向对象)
第二课会介绍Python中的一些变量的使用.列表.元组.字典等一些详细内容...篇幅会比较多...因此在整理中... 先跳过第二课...直接来第三课..Python中面向对象的学习以及与mysql数据 ...