[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[] ...
随机推荐
- bzoj1977 [BeiJing2010组队]次小生成树 Tree——严格次小生成树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1977 因为严格,所以要记录到 LCA 的一个次小值: 很快写好,然后改掉一堆错误后终于过了样 ...
- 如何根据configure.ac和Makefile.am为开源代码产生当前平台的Makefile
1 2 3 4 5 6 7 8 9 //根据configure.in和Makefile.am生成makefile的步骤,基于UBUNTU 12.04 1.autoscan (可选) 2.aclocal ...
- astgo-官方提供的使用技巧大全
Astgo服务器相关: 1.Astgo支持双IP绑定,比如联通.电信IP,Astgo 默认SIP端口 5080和5061 菜单: 选项->系统设置 可以设置以上参数 2.支持RC4加密,自动判断 ...
- [Swift通天遁地]四、网络和线程-(4)使用Alamofire实现网络请求
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- vue打包问题:Tip: built files are meant to be served over an HTTP server.
npm run build之后,出现提示:Tip: built files are meant to be served over an HTTP server. Opening index.html ...
- 如何读取 Json 格式文件
Json 源文件代码: [ { "Id": "0", "Name": "书籍", "Detail": ...
- 【转】linux下passwd命令设置修改用户密码
1.passwd 简单说明: 我们已经学会如何添加用户了,所以我们还要学习设置或修改用户的密码:passwd命令的用法也很多,我们只选如下的几个参数加以说明:想了解更多,请参考man passwd或p ...
- 【转】Linux命令学习手册-split命令
转自:http://blog.chinaunix.net/uid-9525959-id-3054325.html split [OPTION] [INPUT [PREFIX]] [功能]将文件分割成多 ...
- jquery ajax在IE9以下进行跨域请求时无效的问题
第一步:设置浏览器安全属性,启用[通过域访问数据源]选项: 1.选择Internet选项 2.选择安全---自定义级别 3.找到其他---通过域访问数据源,选择启用,然后确定就可以了. 第二步:调用a ...
- python gdal 修改shp文件的属性值
driver = ogr.GetDriverByName('ESRI Shapefile')datasource = driver.Open(shpFileName, 1)layer = dataso ...