[poj 1741]Tree 点分治
题意
求树上距离不超过k的点对数,边权<=1000
题解
点分治。
点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处理,经过就把两边的点瞎那啥统计一下,因为会有完全在子树内的路径,还要容斥算算。
点分治是O(logn)的,但是每次操作是O(nlogn)那么总时间就是
#include<map>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<complex>
#include<iostream>
#include<assert.h>
#include<algorithm>
using namespace std;
#define inf 1001001001
#define infll 1001001001001001001LL
#define ll long long
#define dbg(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define gmax(a,b) (a)=max((a),(b))
#define gmin(a,b) (a)=min((a),(b))
#define Ri register int
#define gc getchar()
#define il inline
il int read(){
bool f=true;Ri x=0;char ch;while(!isdigit(ch=gc))if(ch=='-')f=false;while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=gc;}return f?x:-x;
}
#define gi read()
#define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
struct edge{
int to,next,v;
} e[23333];
int last[20000],cnt;
void link(int a,int b,int c){
e[++cnt]=(edge){b,last[a],c};last[a]=cnt;
e[++cnt]=(edge){a,last[b],c};last[b]=cnt;
}
int siz[23333],f[23333],vis[23333],deep[23333],d[23333],root,sum,ans,n,k;
void zy(int x,int fa=-1){
siz[x]=1;f[x]=0;
for(int i=last[x];i;i=e[i].next){
if(e[i].to!=fa&&!vis[e[i].to]){
zy(e[i].to,x);
siz[x]=siz[x]+siz[e[i].to];
f[x]=max(f[x],siz[e[i].to]);
}
}
f[x]=max(f[x],sum-siz[x]);
if(f[x]<f[root])root=x;
}
void dfs(int x,int fa=-1){
deep[++deep[0]]=d[x];
for(int i=last[x];i;i=e[i].next){
if(e[i].to!=fa&&!vis[e[i].to]){
d[e[i].to]=d[x]+e[i].v;
dfs(e[i].to,x);
}
}
}
int js(int x,int now){
d[x]=now;deep[0]=0;
dfs(x);
sort(deep+1,deep+deep[0]+1);
int zzyy=0,l,r;
for(int l=1,r=deep[0];l<r;){
if(deep[l]+deep[r]<=k)zzyy=zzyy+r-l,l++;
else --r; }//f**k;
return zzyy;
}
void dfz(int x){
ans+=js(x,0);
vis[x]=1;
for(int i=last[x];i;i=e[i].next){
if(!vis[e[i].to]){
ans-=js(e[i].to,e[i].v);
sum=siz[e[i].to];
root=0;
zy(e[i].to,root);
dfz(root);
}
}
}
int main(){
while(n=gi,k=gi,n&&k){
ans=0,cnt=0,root=0;
memset(vis,0,sizeof(vis));
memset(last,0,sizeof(last));
for(int i=1;i<n;i++){
int a,b,c;
a=gi;b=gi;c=gi;
link(a,b,c);
}
sum=n;f[0]=inf;
zy(1);
dfz(root);
printf("%d\n",ans);
}
return 0;
}
[poj 1741]Tree 点分治的更多相关文章
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
随机推荐
- js执行过程
正如我们了解的一样,当我们书写了JS程序之后,打开浏览器,我们的代码就可以开始运行了(当然保证你的代码没有问题,才能按照你的预期进行执行).刚才说的是JS执行的一个大的环境,今天我们学习一下,JS在解 ...
- nginx+php与apache+php性能对比
测试工具http_load相同的动态页面测试,相同的硬件资源,相同并发,相同请求数量的前提下,nginx+php比apache+php的性能要 差,而且如果请求的压力大于硬件资源的承受能力,nginx ...
- MongoDB查询语法
mongoDb是非关系型数据库,用习惯了mssql,mysql等数据库的需要转换一下思维 mongoDb存的是与js的json结构一样的文档,表中的每一条记录都可以结构不同 1,大于,小于,大于等于, ...
- ecshop常用语句
ecshop之中的IF语句: <select name="product_cat" id="product_cat" class="form-c ...
- (转)最强Android模拟器genymotion的安装与配置
Android开发人员都知道,原生的模拟器启动比较慢,操作起来也不流畅,还会出现莫名的问题.当然很多人都会选择直接使用android手机来开发,但是有时候需要在投影仪上演示程序的时候手机不太好做到吧. ...
- 黑白棋游戏 (codevs 2743)题解
[问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...
- Sql Server数据库之通过SqlBulkCopy快速插入大量数据
废话不多说,直接上代码 /// <summary> /// 海量数据插入方法 /// </summary> /// <param name="connectio ...
- 十天学会单片机Day0点亮LED (锁存器、三极管、继电器)
C51常用的数据类型 数据类型 关键字 所占位数 表示数范围 无符号字符型 unsigned char 8 0~255 有符号字符型 char 8 -128~127 无符号整型 unsigned in ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- python datetime date time详解
之前一直被datetime,date,time弄的有点乱,可能是因为看文档每太看明白,找到了两篇文章供大家阅读都是转载的,其中有些名词这里解释一下: 世界协调时间(Universal Time Coo ...