[bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树的值,这样算出的就是与这个重心有关的情况的答案,比如这道题,求路径,那么就考虑在重心所在的子树中所有的路径减去不过重心的路径就是过重心的路径了。之前重心没找对...poj时间卡的紧就T了。。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime> using namespace std; struct Edge
{
int to,next,w;
}e[]; int n,k,cnt,p[],Ans;
int Son[],f[],val[],depth[];
bool visited[]; void Add_edge(const int x,const int y,const int z)
{
e[++cnt].to=y;
e[cnt].next=p[x];
e[cnt].w=z;
p[x]=cnt;
return ;
} void Get_root(const int S,const int fa,const int tot,int & root)
{
Son[S]=,f[S]=;
for(int i=p[S];i;i=e[i].next)
{
if(e[i].to==fa || visited[e[i].to])continue;
Get_root(e[i].to,S,tot,root);
Son[S]+=Son[e[i].to];
f[S]=max(f[S],Son[e[i].to]);
}
f[S]=max(f[S],tot-Son[S]);
if(f[S]<f[root])root=S;
return ;
} void Get_depth(const int S,const int fa)
{
val[++val[]]=depth[S];
for(int i=p[S];i;i=e[i].next)
{
if(e[i].to==fa || visited[e[i].to])continue;
depth[e[i].to]=depth[S]+e[i].w;
Get_depth(e[i].to,S);
}
return ;
} int Calc(const int S,const int w)
{
depth[S]=w,val[]=;
Get_depth(S,);
sort(val+,val+val[]+);
int t=,l,r;
for(l=,r=val[];l<r;)
{
if(val[l]+val[r]<=k)t+=r-l,l++;
else r--;
}
return t;
} void TDC(const int S)
{
Ans+=Calc(S,);
visited[S]=true;
for(int i=p[S];i;i=e[i].next)
{
if(visited[e[i].to])continue;
Ans-=Calc(e[i].to,e[i].w);
int root=;
Get_root(e[i].to,,Son[e[i].to],root);
TDC(root);
}
return ;
} int main()
{
int x,y,z,i,root; while(scanf("%d%d",&n,&k) && n && k)
{
root=,memset(p,,sizeof(p));cnt=;
memset(visited,,sizeof(visited));
Ans=;
for(i=;i<n;++i)
{
scanf("%d%d%d",&x,&y,&z);
Add_edge(x,y,z);
Add_edge(y,x,z);
} f[]=0x3f3f3f3f;
Get_root(,,n,root);
TDC(root); printf("%d\n",Ans);
} return ;
}
[bzoj1468][poj1741]Tree[点分治]的更多相关文章
- [bzoj1468][poj1741]Tree_点分治
Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- [POJ1741]Tree(点分治)
树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...
- 【BZOJ1468】Tree [点分治]
Tree Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...
- [poj1741]Tree(点分治+容斥原理)
题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- 点分治【bzoj1468】 Tree
点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
随机推荐
- POJ 2195 Going Home 最小费用流
POJ2195 裸的最小费用流,当然也可以用KM算法解决,但是比较难写. 注意反向边的距离为正向边的相反数(因此要用SPFA) #include<iostream> #include< ...
- Kconfig详解-文件的基本要素 ***
当执行make menuconfig时会出现内核的配置界面,所有配置工具都是通过读取"arch/$(ARCH)Kconfig"文件来生成配置界面,这个文件就是所有配置的总入口,它会 ...
- 在LNMP或Nginx上配置NameCheap免费SSL证书
- U盘在电脑上安装CentOS 7 系统过程详解
U盘制作CentOS系统启动盘 在电脑上下载并安装UltraISO软件,如百度云:http://pan.baidu.com/s/1hrGtvEG 打开UltraISO软件,找到CentOS.iso的映 ...
- 【转】Java中特殊的String类型
Java中String是一个特殊的包装类数据有两种创建形式: String s = "abc"; String s = new String("abc"); 第 ...
- C#内容格式刷html 转成txt
/// <summary> /// 内容格式刷 /// </summary> /// <param name="strHtml">要格式的文本& ...
- 前端h5开发调试神奇vconsole
(1)项目中安装vconcole插件 npm install vconcole (2)在vue项目中main.js中引入插件 import Vconsole from 'vconsole'; cons ...
- Hash二次探测
Hash的二次探测,当hash的长度为n:插入val,当Hash[val]不为0时,选择新地址newval = val +(-) 1*1,val+(-)2*2,val+(-)(n-1)*(n-1); ...
- SV creation order
SystemVerilog Instance Worlds When generating an UVM testbench and in particular the DUT - testbench ...
- C#连接Sqlite 出现:混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。的解决方案
C#连接Sqlite 出现: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集.的解决方案 C#连接sqlite数据库代码 ...