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求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...
随机推荐
- js:苹果手机页面返回,数据不刷新问题
$(function () { var isPageHide = false; window.addEventListener('pageshow', function () { ...
- Spring-boot中@ConfigurationProperties,@Value,@PropertySource
1.利用@ConfigurationProperties获取配置的值,@ConfigurationProperties是springboot提供的基于安全类型的配置放置. application.pr ...
- Debian 安装 virtualbox
首先增加源: echo "deb http://download.virtualbox.org/virtualbox/debian stretch contrib" > /e ...
- case ··· when ··· then ····的使用,同一字段不同内容分组显示
问题: 查询结果显示成--> sql: SELECT 姓名, SUM(CASE WHEN (课程 = '语文') THEN 分数 ELSE 0 END) AS 语文, SUM(CASE WHEN ...
- myeclise2017安装包及破解文件
亲测可用! myeclipse2017安装包 链接:https://pan.baidu.com/s/13R1wk7mI9ECjEByanIbHDA 提取码:vaeb myeclipse2017破解包 ...
- gcc8.2安装
contrib/download_prerequisites 1.base_url='ftp://gcc.gnu.org/pub/gcc/infrastructure/' 改为base_url='ht ...
- C#使用Spire.Doc Word for .Net读写Word
以前对Excel或Word文档操作都使用微软的COM组件Microsoft Word 15.0 object library. 但是这种方式必须要求服务器上安装Office,而且会出现读写操作完成后未 ...
- ajax请求aspx.cs后台方法
前台jquery代码 $(function () { $("#btnfix").click(function () { $.ajax({ type: "post" ...
- Android 添加第三方jar包
1,拷贝jar包到项目的\app\libs文件夹下. 2,打开项目下的build.gradle(Module:app)文件,在“dependencies {}”中添加“compile files('l ...
- 从零开始学spring cloud(五) -------- 将服务注册到Eureka上
一.开发前准备工作: 官方文档地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RELEASE/mul ...