最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案。

 #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

给你一棵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

bzoj1468 Tree的更多相关文章

  1. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  2. BZOJ1468:Tree(点分治)

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  3. BZOJ1468: Tree & BZOJ3365: [Usaco2004 Feb]Distance Statistics 路程统计

    [传送门:BZOJ1468&BZOJ3365] 简要题意: 给出一棵n个点的树,和每条边的边权,求出有多少个点对的距离<=k 题解: 点分治模板题 点分治的主要步骤: 1.首先选取一个点 ...

  4. 【点分治】bzoj1468 Tree

    同poj1741. 换了个更快的姿势,不会重复统计然后再减掉什么的啦~ #include<cstdio> #include<algorithm> #include<cst ...

  5. 洛谷4178 BZOJ1468 Tree题解点分治

    点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...

  6. 点分治【bzoj1468】 Tree

    点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...

  7. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  8. C++之路进阶——bzoj1468(tree)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  9. 【BZOJ1468】Tree

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

随机推荐

  1. getResourceAsStream和getResource的用法及Demo实例

    用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大家最经常用的,就是用JAVA的File类,如要取得 D:/test.txt文件,就会这样用File file ...

  2. RDBMS DML DDL

    RDBMS RDBMS 指的是关系型数据库管理系统. RDBMS 是 SQL 的基础,同样也是所有现代数据库系统的基础,比如 MS SQL Server, IBM DB2, Oracle, MySQL ...

  3. zabbix注入过程分析

    Zabbix jsrpc.php sql 注入过程分析 漏洞公开详情(https://support.zabbix.com/browse/ZBX-11023)中提示在insertDB() 中的inse ...

  4. IIS ARR 负载均衡

    阅读:http://www.cnblogs.com/jesse2013/p/dlws-loadbalancer2.html 自定义端口:http://www.th7.cn/Program/net/20 ...

  5. 删除Tomcat服务及其它注意

    使用sc delete Tomcat7(注意服务名是Tomcat7 不是Apache......Tomcat7.0). 绿色版无法启动的话,需要先ervice.bat install注册一下服务. 如 ...

  6. 还原MySql数据库失败:max_allowed_packet 设置过小导致记录写入失败

    MySQL根据配置文件会限制Server接受的数据包大小. 有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...

  7. Redis学习 - 配置属性:protected-mode

    根据redis的说明,protected-mode在同时存在如下两种情况时触发: 1) The server is not binding explicitly to a set of address ...

  8. TCP/IP四层模型

    转自:http://www.cnblogs.com/BlueTzar/articles/811160.html ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的 ...

  9. ACM/ICPC 之 混合图的欧拉回路判定-网络流(POJ1637)

    //网络流判定混合图欧拉回路 //通过网络流使得各点的出入度相同则possible,否则impossible //残留网络的权值为可改变方向的次数,即n个双向边则有n次 //Time:157Ms Me ...

  10. javaWeb项目部署到阿里云服务器步骤

    记录web项目部署到阿里云服务器步骤 (使用 web项目.阿里云服务器.Xftp.Xshell),敬请参考和指正 1.将要部署的项目打包成WAR文件格式,可以在MyEclipse.Eclipse都可以 ...