P4178 Tree

最简单的点分治
淀粉质的思想:
“分而治之”,缩小问题规模,合并求解;
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define up(i,l,r) for(register int i = (l); i <= (r); ++i)
#define dn(i,l,r) for(register int i = (l); i >= (r); --i)
#define ll long long
#define re register
using namespace std; template <typename T> void in(T &x) {
x = ; T f = ; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -; ch = getchar();}
while( isdigit(ch)) {x = * x + ch - ; ch = getchar();}
x *= f;
} template <typename T> void out(T x) {
if(x < ) x = -x , putchar('-');
if(x > ) out(x/);
putchar(x% + );
}
//--------------------------------------------------------- const int N = ; struct edge {
int v,w,nxt;
} e[N<<];int tot,head[N]; void add(int u,int v,int w) {
e[++tot].v = v;
e[tot].w = w;
e[tot].nxt = head[u];
head[u] = tot;
}
//--------------------------------------------------------- int n,k,ans;
int size[N];
int Tsize,cnt,rt;
int cdis[N],dis[N];
bool vis[N]; int f[N];
void get_rt(int u,int fa) {
size[u] = ; f[u] = ;
for(re int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v; if(v == fa || vis[v]) continue;
get_rt(v,u);
size[u] += size[v];
f[u] = max(size[v],f[u]);
}
f[u] = max(f[u],Tsize-size[u]);
if(f[u] < f[rt]) rt = u;
} void get_dis(int u,int fa) {
cdis[++cnt] = dis[u];
for(re int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v; if(v == fa || vis[v]) continue;
dis[v] = dis[u] + e[i].w; get_dis(v,u);
}
} int calc(int u) {
cnt = ,get_dis(u,);
sort(cdis+,cdis+cnt+);
int l = ,r = cnt,sum = ;
while(l < r) {
if(cdis[l] + cdis[r] <= k) sum += r-l,++l;
else --r;
}
return sum;
} void new_tree(int u) {
vis[u] = ; dis[u] = ;//
ans += calc(u);
for(re int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v; if(vis[v]) continue;
dis[v] = e[i].w;//
ans -= calc(v);
rt = ,Tsize = size[v];
get_rt(v,); new_tree(rt);
}
} void init() {
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
Tsize = n,rt = ,f[] = n+; ans = ;
} int main() {
freopen("input.txt","r",stdin);
in(n);
init(); int x,y,w;
up(i,,n-) in(x),in(y),in(w),add(x,y,w),add(y,x,w);in(k);
get_rt(,); new_tree(rt);
out(ans); putchar('\n');
}
P4178 Tree的更多相关文章
- luogu P4178 Tree
题目链接 luogu P4178 Tree 题解 点分治 代码 // luogu-judger-enable-o2 #include<cstdio> #include<algorit ...
- 【题解】[P4178 Tree]
[题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...
- POJ1471 Tree/洛谷P4178 Tree
Tree P4178 Tree 点分治板子. 点分治就是直接找树的重心进行暴力计算,每次树的深度不会超过子树深度的\(\frac{1}{2}\),计算完就消除影响,找下一个重心. 所以伪代码: voi ...
- 洛谷P4178 Tree (点分治)
题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入输出格式 输入格式: N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下 ...
- 洛谷 P4178 Tree —— 点分治
题目:https://www.luogu.org/problemnew/show/P4178 这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美: 没有写 sor ...
- [Luogu P4178]Tree (点分治+splay)
题面 传送门:https://www.luogu.org/problemnew/show/P4178 Solution 首先,长成这样的题目一定是淀粉质跑不掉了. 考虑到我们不知道K的大小,我们可以开 ...
- P4178 Tree(点分治)
题面要求小于等于K的路径数目,我么很自然的想到点分治(不会的就戳我) 这道题的统计答案与模板题不一样的地方是由等于K到小于等于K 那么我们可以把每一个子节点到当前根(重心)的距离排序,然后用类似双指针 ...
- 洛谷P4178 Tree (算竞进阶习题)
点分治 还是一道点分治,和前面那道题不同的是求所有距离小于等于k的点对. 如果只是等于k,我们可以把重心的每个子树分开处理,统计之后再合并,这样可以避免答案重复(也就是再同一个子树中出现路径之和为k的 ...
- 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)
推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...
随机推荐
- C# 加载静态资源问题
加载的格式是这样:
- position:fixed失效情况
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PS 知识整理
Ctrl+J键复制背景图层. ps怎样只让截取的一部分图旋转 https://jingyan.baidu.com/article/d45ad14877fc6e69542b8079.html 按Ctrl ...
- 【笔记】Python基础六:模块module介绍及常用模块
一,module模块和包的介绍 1,在Python中,一个.py文件就称之为一个模块(Module). 2,使用模块的好处? 最大的好处是大大提高了代码的可维护性 其次,编写代码不必从零开始,我们编写 ...
- Dubbo 的配置主要分为三大类
服务发现.服务治理和性能调优:这三类配置不是独立存在的,而是贯穿在所有配置项中的,比如dubbo:service 标签中的interface 是服务发现类, timeout是性能调优类, mock 是 ...
- ESP8266 软件实现 Delta-sigma(ΔΣ)调制器 并通过I2S接口输出编码流
一.关于Delta-sigma(ΔΣ)调制器 Delta-sigma(ΔΣ)调制器是Delta-sigma转换器的核心部件.如下所示为一个简单的一阶Delta-sigma调制器,该调制器产生一个1bi ...
- UTF-8和GBK有什么区别
UTF-8和GBK有什么区别 2017年06月03日 18:10:43 阅读数:6516 GBK是在国家标准GB2312基础上扩容后兼容GB2312的标准(好像还不是国家标准).GBK编码专门用来解决 ...
- webpack and publish lib
http://keer2345.github.io/2018/04/13/webpack-4-tutorial-example-with-npm/ https://blog.csdn.net/feng ...
- Spring常用注解总结(3)
@Configuration 表示该类为"配置类",可替换xml配置文件.与@Component不同的是,@Configuration会生成CGLIB代理class. @Targe ...
- vue全局API
一.Vue.extend() 顾名思义 extend 继承,官方给出的解释是 (使用基础 Vue 构造器,创建一个“子类”.参数是一个包含组件选项的对象.) Vue构造器是指 vue是一个构 ...