2018.07.20 洛谷P4178 Tree(点分治)
传送门
又一道点分治。
直接维护子树内到根的所有路径长度,然后排序+双指针统计答案。
代码如下:
#include<bits/stdc++.h>
#define N 40005
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
int rt,sum,ans,n,k,tot=0,cnt=0,msiz[N],siz[N],d[N],first[N];
bool vis[N];
struct Node{int v,next,w;}e[N<<1];
inline void add(int u,int v,int w){e[++tot].v=v,e[tot].w=w,e[tot].next=first[u],first[u]=tot;}
inline void getroot(int p,int fa){
siz[p]=1,msiz[p]=0;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(v==fa||vis[v])continue;
getroot(v,p),siz[p]+=siz[v];
if(msiz[p]<siz[v])msiz[p]=siz[v];
}
if(sum-siz[p]>msiz[p])msiz[p]=sum-siz[p];
if(msiz[p]<msiz[rt])rt=p;
}
inline void getdis(int p,int fa,int dis){
d[++cnt]=dis;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(vis[v]||v==fa)continue;
getdis(v,p,dis+e[i].w);
}
}
inline int calc(int p,int v){
int ret=0;
cnt=0;
getdis(p,0,v);
sort(d+1,d+cnt+1);
int l=1,r=cnt;
while(l<r){
while(d[l]+d[r]>k&&r>l)--r;
if(d[l]+d[r]<=k)ret+=r-l;
++l;
}
return ret;
}
inline void solve(int p){
ans+=calc(p,0);
vis[p]=true;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
if(vis[v])continue;
ans-=calc(v,e[i].w);
sum=siz[v],rt=0,getroot(v,0);
solve(rt);
}
}
int main(){
n=read();
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
}
k=read();
msiz[rt=0]=sum=n,getroot(1,0),solve(rt);
printf("%d",ans);
return 0;
}
2018.07.20 洛谷P4178 Tree(点分治)的更多相关文章
- 洛谷P4178 Tree (点分治)
题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入输出格式 输入格式: N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下 ...
- 洛谷 P4178 Tree —— 点分治
题目:https://www.luogu.org/problemnew/show/P4178 这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美: 没有写 sor ...
- [洛谷P4178] Tree (点分治模板)
题目略了吧,就是一棵树上有多少个点对之间的距离 \(\leq k\) \(n \leq 40000\) 算法 首先有一个 \(O(n^2)\) 的做法,枚举每一个点为起点,\(dfs\) 一遍可知其它 ...
- POJ1471 Tree/洛谷P4178 Tree
Tree P4178 Tree 点分治板子. 点分治就是直接找树的重心进行暴力计算,每次树的深度不会超过子树深度的\(\frac{1}{2}\),计算完就消除影响,找下一个重心. 所以伪代码: voi ...
- 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)
P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...
- 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)
推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...
- 2018.07.17 洛谷P1368 工艺(最小表示法)
传送门 好的一道最小表示法的裸板,感觉跑起来贼快(写博客时评测速度洛谷第二),这里简单讲讲最小表示法的实现. 首先我们将数组复制一遍接到原数组队尾,然后维护左右指针分别表示两个即将进行比较的字符串的头 ...
- 2018.07.22 洛谷P3047附近的牛(树形dp)
传送门 给出一棵n" role="presentation" style="position: relative;">nn个点的树,每个点上有C ...
- 2018.07.22 洛谷P1967 货车运输(kruskal重构树)
传送门 这道题以前只会树剖和最小生成树+倍增. 而现在学习了一个叫做kruskal" role="presentation" style="position: ...
随机推荐
- SQL 2012 分页取数据
,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...
- VBA 操作 VBE
Introduction You can write code in VBA that reads or modifies other VBA projects, modules, or proced ...
- 进程队列(Queue),Pipe(管道), Manager 进行进程之间的数据传递和传输
进程Queue,实现进程传输的队列 1.Queue from multiprocessing import Process, Queue def f(q): q.put('1') q.put('2') ...
- Producer-consumer problem in Python
from: http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/ By : Akshar Raaj We will s ...
- TPCC-MySQL的安装与使用
Tpcc-mysql 是 percona 基于 tpcc 衍生出来的产品,专用于 mysql 基准测试,其源码放在 bazaar 上( Bazaar 是一个分布式的版本控制系统,采用 GPL 许可协议 ...
- HTML meta 文本 格式排版 链接图表 列表 表单 frame后台布局实例
meta标签 content属性必须和http-equiv或者name属性一起使用 http-equiv属性,就是http当量,用于和服务器发送数据前的提交交互使用.(另层含义这个当量在浏览器和服务器 ...
- SMO算法(转)
作者:[已重置]链接:https://www.zhihu.com/question/40546280/answer/88539689来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 代码重构:用工厂+策略模式优化过多的if else代码块
最近在工作中优化了一段冗余的if else代码块,感觉对设计模式的理解和运用很有帮助,所以分享出来.鉴于原代码会涉及到公司的隐私,因此就不贴出来了.下面以更加通俗易懂的案例来解析. 假如写一个针对员工 ...
- 安装Anaconda以及jupyter的使用
1)下载https://www.anaconda.com/download/ 2)安装 3)终端查看(Anaconda Prompt) 4)升级所有的包 初次安装下的软件包版本一般都比较老旧,因此提前 ...
- crsf 跨站请求伪造
[crsf 跨站请求伪造] CSRF(Cross-site request forgery),中文名称:跨站请求伪造.核心为利用浏览器帮助提交cookie.采用随机数方可防御.估计大部小站均无CSRF ...