【刷题】BZOJ 1468 Tree
Description
给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K
Input
N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k
Output
一行,有多少对点之间的距离小于等于k
Sample Input
7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
5
Solution
开始专做点分治的题目了
这就是点分治的裸题了吧
找到重心并分治之后,维护的是与根的距离
对于每个根,求的是路径经过根的两点距离小于 \(k\) 的方案数
注意去重
calc中如果有两点在同一子树内,那么它们在当前calc中信息是假的(它们本身之间的距离并没有那么大),所以在访问这个子树之前要先把多算的减掉
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=40000+10,inf=0x3f3f3f3f;
int n,k,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],deep[MAXN],cnt,Msonsize[MAXN],size[MAXN],d[MAXN],root,finish[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
w[e]=z;
}
inline void getroot(int x,int f,int ntotal)
{
Msonsize[x]=0;size[x]=1;
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f||finish[to[i]])continue;
else
{
getroot(to[i],x,ntotal);
size[x]+=size[to[i]];
chkmax(Msonsize[x],size[to[i]]);
}
chkmax(Msonsize[x],ntotal-size[x]);
if(Msonsize[x]<Msonsize[root])root=x;
}
inline void getdeep(int x,int f)
{
deep[++cnt]=d[x];
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f||finish[to[i]])continue;
else d[to[i]]=d[x]+w[i],getdeep(to[i],x);
}
inline int calc(int x,int st)
{
d[x]=st;cnt=0;
getdeep(x,0);
std::sort(deep+1,deep+cnt+1);
int l=1,r=cnt,ans=0;
while(l<r)
{
if(deep[l]+deep[r]<=k)ans+=r-l,l++;
else r--;
}
return ans;
}
inline int solve(int x)
{
int res=calc(x,0);
finish[x]=1;
for(register int i=beg[x];i;i=nex[i])
if(!finish[to[i]])
{
res-=calc(to[i],w[i]);
root=0;
getroot(to[i],x,size[to[i]]);
res+=solve(root);
}
return res;
}
int main()
{
read(n);
for(register int i=1;i<n;++i)
{
int u,v,w;
read(u);read(v);read(w);
insert(u,v,w);insert(v,u,w);
}
read(k);
Msonsize[0]=inf;root=0;
getroot(1,0,n);
write(solve(root),'\n');
return 0;
}
【刷题】BZOJ 1468 Tree的更多相关文章
- bzoj 1468 Tree(点分治模板)
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1527 Solved: 818[Submit][Status][Discuss] ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
- BZOJ 1468: Tree
Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- [刷题] PTA 03-树3 Tree Traversals Again
用栈实现树遍历 1 #include<stdio.h> 2 #include<string.h> 3 #define MAXSIZE 30 4 5 int Pre[MAXSIZ ...
- [刷题] 257 Binary Tree Paths
要求 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串 示例 ["1->2->5","1->3"] 思路 递归地返回左右子树到叶子节 ...
随机推荐
- Eclipse 使用过程中的问题及解决方法
1.Eclipse中java文件和jsp字体大小设置 1.更改所有文件的字体显示大小过程: Window->preferences->General->Appearance-> ...
- Appium+python 自动发送邮件(2)(转)
(原文:https://www.cnblogs.com/fancy0158/p/10056418.html) 移动端执行完测试case之后,通过邮件自动发送测试报告.大体流程如下: 1.通过unitt ...
- HIVE简介及安装
一.简介 百度百科HIVE定义: hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运 ...
- 高可用Kubernetes集群-2. ca证书与秘钥
四.CA证书与秘钥 kubernetes集群安全访问有两种方式:"基于CA签名的双向数字证书认证"与"基于BASE或TOKEN的简单认证",生产环境推荐使用&q ...
- YAML 基础
YAML 基础 简介 对象 数组 常量 引用 1. 简介 YAML 是专门用来写配置文件的语言,非常简洁和强大! 它的基本语法规则有: 大小写敏感: 使用缩进表示层级关系: 缩进时不允许使用 Tab ...
- 使用SKlearn(Sci-Kit Learn)进行SVR模型学习
今天了解到sklearn这个库,简直太酷炫,一行代码完成机器学习. 贴一个自动生成数据,SVR进行数据拟合的代码,附带网格搜索(GridSearch, 帮助你选择合适的参数)以及模型保存.读取以及结果 ...
- [T-ARA][Lovey-Dovey]
歌词来源:http://music.163.com/#/song?id=22704426 作曲 : 新沙洞老虎/崔圭成 [作曲 : 新沙洞老虎/崔圭成] [作曲 : 新沙洞老虎/崔圭成] 作词 : 新 ...
- windows8和windows server2012不联网安装.net 3.5(包括2.0和3.0)
安装完win8后 发现系统默认没有安装.net3.5 如果使用在线更新的话需要很久才能完成,特别是当前的网速以及微软的服务器.速度很忙,其实我们利用win8的安装盘就可以不需要联网更新,而且几分钟就搞 ...
- ORA-28000: the account is locked 查哪个具体ip地址造成
查系统默认的策略,连续验证10次错误帐户即会被锁 SQL> select resource_name, limit from dba_profiles where profile='DEFAUL ...
- Android:有关下拉菜单导航的学习(供自己参考)
Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...