bzoj1468
1468: Tree
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1023 Solved: 532
[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
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的更多相关文章
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- 点分治【bzoj1468】 Tree
点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...
- [bzoj1468][poj1741]Tree_点分治
Tree bzoj-1468 poj-1741 题目大意:给你一颗n个点的树,求树上所有路径边权和不大于m的路径条数. 注释:$1\le n\le 4\cdot 10^4$,$1\le m \le 1 ...
- BZOJ1468: Tree & BZOJ3365: [Usaco2004 Feb]Distance Statistics 路程统计
[传送门:BZOJ1468&BZOJ3365] 简要题意: 给出一棵n个点的树,和每条边的边权,求出有多少个点对的距离<=k 题解: 点分治模板题 点分治的主要步骤: 1.首先选取一个点 ...
- bzoj1468 Tree
最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...
- 【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行边描述管道,按照题目中写的输入 接下来是 ...
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- PAT1016
A long-distance telephone company charges its customers by the following rules: 一个长途电话公司费用告诉它的顾客需要遵循 ...
- Android如何使用API
转自:http://www.cnblogs.com/vanezkw/archive/2012/07/03/2574559.html 本文针对Android开发如何使用API文档进行一些经验分享. 1. ...
- java集合框架list和set
为什么出现集合类? 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一中方式 数组和集合类同是容器,有何不同? 数组虽然也可以存储对象 ...
- 理解用requireJs 来实现javascript的模块化加载
这是我看到的一片关于requirejs的初学者的文章,写的不错,下面结合自己的理解记录一下: 原文:http://www.sitepoint.com/understanding-requirejs-f ...
- FusionCharts使用问题及解决方法(二)-FusionCharts常见问题大全
在上文中,我们介绍了FusionCharts常见问题(FAQ)的解决方法,本文将一同讨论FusionCharts使用者面临的一些复杂问题的解决方法. 如何启用JavaScript调试模式? 要启用Ja ...
- 直接拿来用!最火的iOS开源项目(一)
直接拿来用!最火的iOS开源项目(一) 发表于2013-06-05 10:17| 39373次阅读| 来源CSDN| 100 条评论| 作者唐小引 iOS开源项目GitHub移动开发最受欢迎的开源项目 ...
- ASP.NET WebService
一.WebService:WebService是以独立于平台的方式,通过标准的Web协议,可以由程序访问的应用程序逻辑单元. (1)应用程序逻辑单元:web服务包括一些应用程序逻辑单元或者代码.这些代 ...
- Android 手机红外遥控器实现(转)
源:http://www.cnblogs.com/xiaochao1234/p/3615467.html 经过连续几天的编制,安卓手机代码终于完成了,目前已经将我宿舍,家里,集控室的红外遥控电气设备完 ...
- Bessie Goes Moo
Bessie Goes Moo 题目描述 Farmer John and Bessie the cow love to exchange math puzzles in their free time ...
- web项目编译出错时,原因之一,可能是build path 中order and Export引起
build path中的order and Export,如果两个libarary中有相同功能的jar包,则编译器会选择顺序在前的jar包中相应的类作为编译所需. 所以,当项目jar包较多的时候,如果 ...