bzoj1468 Tree
最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#define ll long long
#define N 40005
using namespace std;
struct Node{
int to,next,v;
}e[N<<];
int sum,rt,n,k,head[N],tot,cnt,mx[N],sz[N],tmp[N],ans;
bool vis[N];
void add(int x,int y,int z){
e[++tot]=(Node){y,head[x],z};head[x]=tot;
e[++tot]=(Node){x,head[y],z};head[y]=tot;
}
void getroot(int x,int fa){
mx[x]=;sz[x]=;
for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa&&!vis[e[i].to]){
getroot(e[i].to,x);
sz[x]+=sz[e[i].to];
mx[x]=max(mx[x],sz[e[i].to]);
}mx[x]=max(mx[x],sum-sz[x]);
if(mx[x]<mx[rt])rt=x;
}
void getdis(int x,int fa,int d){
tmp[++cnt]=d;
for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa&&!vis[e[i].to]){
getdis(e[i].to,x,d+e[i].v);
}
}
void calc(int x,int dis,int type){
cnt=;
getdis(x,,);
sort(tmp+,tmp++cnt);
int l=,r=cnt;
while(l<=r){
while(tmp[l]+tmp[r]>dis)r--;
if(l>r)break;
ans+=(r-l)*type;l++;
}
}
void work(int x){
vis[x]=;
calc(x,k,);
for(int i=head[x];i;i=e[i].next)if(!vis[e[i].to]){
calc(e[i].to,k-e[i].v*,-);
sum=sz[e[i].to];rt=;
getroot(e[i].to,);
work(rt);
}
}
int main(){
// freopen("test.in","r",stdin);
scanf("%d",&n);
for(int i=;i<n;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
scanf("%d",&k);
sum=mx[]=n;rt=;ans=;
getroot(,);
work(rt);
printf("%d\n",ans);
}
1468: Tree
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1099 Solved: 581
[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
bzoj1468 Tree的更多相关文章
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- BZOJ1468: Tree & BZOJ3365: [Usaco2004 Feb]Distance Statistics 路程统计
[传送门:BZOJ1468&BZOJ3365] 简要题意: 给出一棵n个点的树,和每条边的边权,求出有多少个点对的距离<=k 题解: 点分治模板题 点分治的主要步骤: 1.首先选取一个点 ...
- 【点分治】bzoj1468 Tree
同poj1741. 换了个更快的姿势,不会重复统计然后再减掉什么的啦~ #include<cstdio> #include<algorithm> #include<cst ...
- 洛谷4178 BZOJ1468 Tree题解点分治
点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...
- 点分治【bzoj1468】 Tree
点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- C++之路进阶——bzoj1468(tree)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser gryz2016 Logout 捐赠本站 Notice:由于本OJ ...
- 【BZOJ1468】Tree
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- Unity Development with VS Code
https://code.visualstudio.com/Docs/runtimes/unity
- Microsoft SQL Server 2008 R2官方中文版(SQL2008下载).rar
Microsoft SQL Server 2008 R2官方中文版(SQL2008下载).rar
- 浅谈Android中拍照、从相册选择图片并截图相关知识点
前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...
- 什么是UIScrollView
移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限 当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容 普通的UIView不具备滚动功能,不适合显示过多的内容 ...
- Sublime编辑器安装使用
用习惯了VS2010强大的IDE工具,但也被它折腾过.烦恼过,当vs加载超过万行的脚本代码时,界面半天才反应,经常卡死,电脑配置决定算得上顶呱呱. 不喜欢逆来顺受,于是选择了txt文本编辑器,最原始的 ...
- C#高级编程笔记 Day 5, 2016年9月 13日 (泛型)
[重点]泛型:有了泛型,就可以创建独立于被包含类型的类和方法了.我们不必给不同的类型编写功能相同的许多方法和类,只创建一个方法或类即可,以下是泛型的特点:性能.类型安全性.二进制代码重用.代码的扩展. ...
- 5.openstack之mitaka搭建计算节点
部署计算节点(compute服务) 一:控制节点配置 1.建库建用户 CREATE DATABASE nova_api; CREATE DATABASE nova; GRANT ALL PRIVILE ...
- ASP.NET MVC随想录——漫谈OWIN
什么是OWIN OWIN是Open Web Server Interface for .NET的首字母缩写,他的定义如下: OWIN在.NET Web Servers与Web Application之 ...
- java冒泡排序
public class BubbleSort { public static void main(String[] args) { int score[] = {1,4,5,7,2,3,9,0,6, ...
- Memcache之内存分配机制
可参见:http://blog.csdn.net/hguisu/article/details/7353482