【BZOJ1468】Tree [点分治]
Tree
Time Limit: 10 Sec Memory Limit: 64 MB
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
HINT
Solution
树上路径统计问题,运用点分。
每次处理与重心相关的路径,发现如果直接处理两点之间比较困难,我们想到了将所有点加入一个数组,用指针判断加起来<=K的个数,这样的话不一定全是简单路径,但是我们只要减去每个子树中这样操作的条数就一定只剩下简单路径了。
点分大概的步骤:
1.找出重心;
2.计算经过该重心的路径相关需要求的;
3.去掉重心对于每棵子树继续做以上过程。
Code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std; const int ONE=; int n,K;
int x,y,z;
int next[ONE],first[ONE],go[ONE],w[ONE],tot;
int center_vis[ONE];
int Max,dist[ONE],num;
int d[ONE];
int center,Ans; int get()
{
int res,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} int Add(int u,int v,int z)
{
next[++tot]=first[u]; first[u]=tot; go[tot]=v; w[tot]=z;
next[++tot]=first[v]; first[v]=tot; go[tot]=u; w[tot]=z;
} namespace PointF
{
struct power
{
int maxx,size;
}S[ONE]; void Getsize(int u,int father)
{
S[u].size=;
S[u].maxx=;
for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(v==father || center_vis[v]) continue;
Getsize(v,u);
S[u].size+=S[v].size;
S[u].maxx=max(S[u].maxx,S[v].size);
}
} void Getcenter(int u,int father,int total)
{
S[u].maxx=max(S[u].maxx,total-S[u].size);
if(Max>S[u].maxx)
{
Max=S[u].maxx;
center=u;
} for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(v==father || center_vis[v]) continue;
Getcenter(v,u,total);
}
} void Getdist(int u,int father,int value)
{
dist[++num]=value;
for(int e=first[u];e;e=next[e])
{
int v=go[e];
if(v==father || center_vis[v]) continue;
Getdist(v,u,value+w[e]);
}
} int Calc(int u,int value)
{
int res=;
num=; Getdist(u,,value);
sort(dist+,dist+num+); int l=,r=num; while(l<r)
{
while(dist[l]+dist[r]>K && l<r) r--;
res+=r-l;
l++;
}
return res;
} void Dfs(int u)
{
Max=n;
Getsize(u,); center=u;
Getcenter(u,,S[u].size);
center_vis[center]=true; Ans+=Calc(center,);
for(int e=first[center];e;e=next[e])
{
int v=go[e];
if(center_vis[v]) continue;
Ans-=Calc(v,w[e]);
Dfs(v);
}
}
} int main()
{
n=get();
for(int i=;i<n;i++)
{
x=get(); y=get(); z=get();
Add(x,y,z);
} K=get();
PointF::Dfs(); printf("%d",Ans);
}
【BZOJ1468】Tree [点分治]的更多相关文章
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...
- 洛谷4178 BZOJ1468 Tree题解点分治
点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...
- 【点分治】bzoj1468 Tree
同poj1741. 换了个更快的姿势,不会重复统计然后再减掉什么的啦~ #include<cstdio> #include<algorithm> #include<cst ...
- bzoj1468 Tree
最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
随机推荐
- Android中通过拨号调起应用的实现方式及特殊情况处理
Android中有时我们会有这样的需求:通过拨号调起我们的程序.这个需求如何实现呢? 思路当然是在我们的应用中实现一个广播接收器(BroadcastReceiver),对打电话时系统发出的广播进行拦截 ...
- jmeter插件之jsonpath提取响应结果和做断言
准备工作: 1. jmeter3.X已经自带了提取响应结果的插件:JSON Extractor 2. 下载断言插件:https://jmeter-plugins.org/wiki/JSONPathAs ...
- Mac下用tomcat搭建下载服务器
1.下载tomcat 去官方网址: http://tomcat.apache.org/ 下载最新版 2.下载解压后,自己可以随便放在哪个文件夹下,自己记得路径即可.比如Users/你的用户名/Docu ...
- python之pyquery库
如果熟悉jquery,那么使用pyquery进行网页信息提取将会是非常方便的,因为语法都是一样的 1.字符串初始化 2.url和文件初始化 3.CSS选择器 4.子元素 5.父元素 6.兄弟节点 7. ...
- ubutu下source命令问题(复制)
最近一段时间在使用Bash on Ubuntu on Windows做shell脚本调试时发现在脚本中使用source时会报错,上网查了下才了解到原来是在Ubuntu中使用的并不是bash,而是使用 ...
- mysql ibd 文件还原数据
-- 这里要还原的表名为 test_table -- 1建库,并选中库,库名随意 -- 2查看InnoDB 引擎独立表空间是否开启 SHOW VARIABLES LIKE '%per_table%' ...
- 基于Thinkphp5+phpQuery 网络爬虫抓取数据接口,统一输出接口数据api
TP5_Splider 一个基于Thinkphp5+phpQuery 网络爬虫抓取数据接口 统一输出接口数据api.适合正在学习Vue,AngularJs框架学习 开发demo,需要接口并保证接口不跨 ...
- 高级C代码的汇编分析
在windows上,常用的函数调用方式有: Pascal方式,WINAPI(_stdcall)方式 和C方式(_cdecl) _cdecl调用规则: 1,参数从右到左入堆栈 2,在函数返回后,调用者要 ...
- Nginx学习笔记之加强篇
在上一篇文章Nginx学习笔记之应用篇中,我们已经可以正式运行自己的网站了.但是在使用Nginx服务器时还需要注意几个问题: 1.Nginx服务器上配置的单个站点的并发量不超过1024 2.Nginx ...
- mongo db 使用方法[转]
1 下载 mogodb http://www.mongodb.org/display/DOCS/Downloads 2 打开服务 我安装在e盘下了 可以指定数据文件位置 到 E:\mongoDB\mo ...