1468: Tree

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1023  Solved: 532
[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

HINT

 

Source

题解:

  点分治裸题,那么什么是点分治呢

  点分治

  1、求树的重心
  2、计算以当前重心为根的子树的答案
  3、去掉以当前重心儿子为根的子树的答案
  4、枚举每个儿子,分治

代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 40005
using namespace std;
int n,k,i,j,tot,root,ans,L,all;
int pre[maxn*],now[maxn],v[maxn*],val[maxn*],son[maxn],f[maxn];
int dist[maxn],data[maxn];
bool vis[maxn];
int read()
{
int x=; char ch; bool bo=;
while (ch=getchar(),ch<''||ch>'') if (ch=='-') bo=;
while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
if (bo) return -x; return x;
}
void ins(int a,int b,int c){
pre[++tot]=now[a]; now[a]=tot; v[tot]=b; val[tot]=c;
}
void get_root(int u,int fa)
{
son[u]=; f[u]=;
for (int p=now[u]; p; p=pre[p])
{
int vv=v[p];
if (vv==fa || vis[vv]) continue;
get_root(vv,u); son[u]+=son[vv];
f[u]=max(f[u],son[vv]);
}
f[u]=max(f[u],all-son[u]);
if (f[u]<f[root]) root=u;
}
void get_array(int u,int fa)
{
data[++L]=dist[u];
for (int p=now[u]; p; p=pre[p])
{
int vv=v[p];
if (vv!=fa&&!vis[vv])
dist[vv]=dist[u]+val[p],get_array(vv,u);
}
}
int calc(int u,int value)
{
dist[u]=value; L=; get_array(u,-);
sort(data+,data++L);
int preans=,l=,r=L;
while (l<r)
{
if (data[l]+data[r]<=k) preans+=(r-l),l++; else r--;
}
return preans;
}
void solve(int u)
{
ans+=calc(u,); vis[u]=;
//cout<<" ans "<<u<<endl;
for (int p=now[u]; p; p=pre[p])
{
int vv=v[p];
//cout<<" "<<vv<<endl;
if (vis[vv]) continue;
ans-=calc(vv,val[p]); f[root=]=n+;
all=son[vv]; get_root(vv,-);
solve(root);
}
}
int main()
{
n=read();
for (int i=; i<=n-; i++)
{
int u=read(),v=read(),value=read();
ins(u,v,value); ins(v,u,value);
}
k=read();
f[root=]=n+; all=n;
get_root(,-);
//cout<<" "<<root<<endl;
solve(root);
printf("%d\n",ans);
return ;
}

  

bzoj1468的更多相关文章

  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

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

  3. [bzoj1468][poj1741]Tree_点分治

    Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...

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

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

  5. bzoj1468 Tree

    最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...

  6. 【BZOJ-1468】Tree 树分治

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

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

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

  8. 【BZOJ1468】Tree

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

  9. BZOJ1468:Tree(点分治)

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

随机推荐

  1. ListView使用的时候遇到的一些问题

    昨天在做项目时,请求服务器的好友动态后,将好友动态和评论显示到界面上,用ListView显示,发现一进这个界面时,listView的适配器的getVIew()方法就会执行6次,后来发现原来是ListV ...

  2. opencart配置United States Postal Service快递

    1.安装United States Postal Service 2.登录https://registration.shippingapis.com/,注册帐号,稍后会收到邮件 3.打开邮件,记下Us ...

  3. Linux运维常用的命令详解

    1. 查看系统内核版本 [root@funsion geekxa]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m 显示了系 ...

  4. gridview中button事件处理

    http://msdn.microsoft.com/zh-cn/library/bb907626.aspx 再结合如下: protected void GridView1_RowCommand(obj ...

  5. The Triangle 经典DP

    题意:数塔问题 思路:1:递归.2:递推.3:记忆化搜索.<刘汝佳,第九章> #include<iostream> #include<cstdio> #includ ...

  6. JAVA的RSS处理

    一:什么是RSS RSS(really simple syndication) :网页内容聚合器.RSS的格式是XML.必须符合XML 1.0规范. RSS的作用:订阅BLOG,订阅新闻二:RSS的历 ...

  7. 清除浮动的方法 after伪类。

    .clearfix{ *zoom:1; } .clearfix:after{ content: ""; display: block; clear: both; } 在样式中加入上 ...

  8. ExtJS4.1自带API打不开的问题解决

    在ext官网个下载的最新版本的extjs,本来想看看里面的docs文档的,结果却发现打不开,总是转个不停,于是就打开index.html的源码,看到引入ext的js文件的时候,看到引入的是ext.js ...

  9. OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波

    http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...

  10. 用Date.ToString()输出中英文月份

    DateTime.Now.ToString("dddd,dd MMMM,yyyy")//输出   星期三,30 一月,2008DateTime.Now.ToString(" ...