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求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...
随机推荐
- Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty 报错
项目中遇到的获取https的数据接口数据时,Unexpected error: java.security.InvalidAlgorithmParameterException: the trustA ...
- Unity添加多个可视镜头Preview功能(二)
制作好并摆放好镜头以后,在Preview.cs里添加一个time单个镜头移动时间的变量,并在PreviewEditor下绘制在Inspector面板下. 然后,添加一个FollowPreviewPat ...
- Java实现微信客户端扫码登录
此篇文章记录自己开发中的微信客户端扫码登录的实例以及步骤,便于以后自行学习记起的关键,看到的网友有借鉴的地方就借鉴,看不懂的也请别吐槽,毕竟每个人的思维和思路以及记录东西的方式不一样: 1.首先需要一 ...
- eclipse配置maven仓库
在eclipse中搭建maven工程时,首先需要搭建好maven本地仓库,搭建过程比较简单 1.首先,在eclipse中,打开windows->Maven->User Settings; ...
- React—Native开发之 Could not connect to development server(Android)解决方法
作为初学者昨天还好好能跑的项目今天就会遇到突然爆红出错是经常的事,让我们来看下是什么错吧 先来翻译: 连接不到开发的服务器. 请按照以下的步骤来修复此问题: 确保包服务器在运行确保你的设备或者模拟器连 ...
- del_cursor 批量删除游标
declare cursor [del_cursor] is select a.*, a.rowid row_id from [table_name] a order by a.rowid; ...
- Mysql设置大小写敏感
1.linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写: 2.用root帐号登录后,在/etc/my.cnf 中的[mysqld]后添加添加lower_case_table_n ...
- 红黑树(red-black tree)实现记录
https://github.com/xieqing/red-black-tree A Red-black Tree Implementation In C There are several cho ...
- Python下划线简介
Python中下划线的5种含义 分享一篇文章:The Meaning of Underscores in Python. 本文介绍了Python中单下划线和双下划线("dunder" ...
- 我们一起踩过的坑----react(antd)(一)
1.}] && ){ ){ ){ ||){ ){ );); , }; }); }, beforeUpload: (file) => { ...