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行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- 百度地图API简单应用
在做移动端应用时经常用到百度地图API,百度API有强大的示例和文档,开发之前去百度相关网站注册密钥,很块博主只花了几分钟 百度地图API范例 百度地图API文档说明 例子1:输入特定关键字绘制地图标 ...
- Shell case esac语句
case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构. case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: ...
- Mac中安装Vim7.4
Mac上的Vim Mac本身其实是预装了Vim的,但是目前的系统中都是Vim7.3版本的,而最新的Vim已经是7.4版了,因此为了能够使用最新版的vim,必须要对Mac中的vim要么升级,要么重装.在 ...
- git 简单使用
创建新分支:git branch branchName 切换到新分支:git checkout branchName 然后 ,上面两个命令也可以合成为一个命令: git checkout -b bra ...
- BZOJ3436——小K的农场
1.题意:大概是给一些制约限制,问是否存在合法解 2.分析:我们来观察这三个限制 农场a比农场b至少多种植了c个单位的作物 可以变成b 比 a至多多种了-c 农场a比农场b至多多种植了c个单位 ...
- TCP/IP四层模型
转自:http://www.cnblogs.com/BlueTzar/articles/811160.html ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的 ...
- 20.SqlServer中if跟循环语句
--if语句declare @i int begin print @i end else --循环语句 declare @i int begin insert into grade(classname ...
- 第一天:安装nodejs
1.首先,下载NodeJs程序.地址 http://www.nodejs.org/download/,选择Windows Installer 64bit 2.下载下来后,狂点下一步,安装在本地硬盘上. ...
- How to Install The Alpha Control Packages.
Uninstalling previous version of the package If you have a previous version of the package already i ...
- 19. Remove Nth Node From End of List
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...