【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 ...
随机推荐
- java容器操作一
List l = new ArrayList(); l.add(1); l.add("ne"); // 获取 System.out.println(l.get(0)); // 判断 ...
- 《python核心编程第二版》第1章练习
1–1. 安装 Python.请检查 Python 是否已经安装到你的系统上,如果没有,请下载并 安装它 略 1–2. 执行 Python.有多少种运行 Python 的不同方法?你喜欢哪一种?为什 ...
- 安装配置hadoop
在master中安装并且配置hadoop (1).将hadoop-2.6.4.tar.gz安装包复制到hadoop文件目录下(直接赋值进去就行) (2).解压安装包haoddp-2.6.4.tar.g ...
- 容器基础(十): 使用kubernetes部署应用
概述 使用之前的脚本(env/server.py 得到 env/server:v0.1 镜像, env/worker.py 得到 env/worker:v0.1)得到的镜像,在部署好kubernete ...
- get? post? put? delete? head? trace? options? http请求方法
http1.1协议里面定义了八种请求方法: get:用作获取,读取数据 post:向指定的资源提交数据 put:更新,向指定的资源上传一个内容,比如说:更新一个用户的头像或者替换掉已有的一个视频 de ...
- pta结构体链表作业
一.PTA实验作业 7-2 1. 本题PTA提交列表 2. 设计思路 题目要求按照年龄从大到小的顺序依次输出,于是我只要用冒泡法把结构体变量从大到小排列就可以了. 3.本题调试过程碰到问题及PTA提交 ...
- (转)Nginx配置和内核优化 实现突破十万并发
nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_affinity 00000001 00 ...
- 一种保持顺序的Properties
其实properties有没有顺序都一样 程序都能正常运行 但看着就比较闹心 所以网上找了找 还真有人给了个例子实现读Property的有序 但是删除某些属性之后 写入又有问题 会异常 后来重写了一下 ...
- SQL Server 部署CLR程序集错误`6218`
Visual Studio 2015中开发的SQL Server项目,添加了用户自定义函数,需要部署到SQL Server 2005上, 在部署时报错: (70,1): SQL72014: .Net ...
- doget,doPost在底层走的是service
doget,doPost在底层走的是service 因为在源码上 先执行service方法 然后再调用doget,doPost方法