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行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- Kth order statistcs
Selection: selection is a trivial problem if the input numbers are sorted. If we use a sorting algor ...
- 2016大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- LINQ To SQL && Lambda 使用方法小结 (转)
1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq: from s in Students ...
- 你的float用对了吗
介绍 很多人都知道float是浮点类型,它不能表示数据范围内的所有数值.但是,实际使用或编码时,你又是否记得这句话呢?下面是stackoverflow中的一个问题: why does a float ...
- HTTP缓存控制总结
引言 通过网络获取内容既缓慢,成本又高.大的相应需要在客户端和服务器之间多次往返通信,这拖延了浏览器可以使用和处理内容的时间,同时也增加了通信的成本.因此,缓存和重用以前获取到的资源的能力成为性能优化 ...
- flex中form表单中子元素之间的距离控制
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.ado ...
- 中文字符 unicode转utf-8函数 python实现
unicode编码范围 00000000-0000007F的字符,用单个字节来表示: 00000080-000007FF的字符用两个字节表示 (中文的编码范围) 00000800-0000FFFF的字 ...
- 安装PIL遇到的问题
配置:Win7 64位 不过折腾到最后,没有使用PIL,官方的PIL很久木有更新了,换了Pillow,是PIL的衍生吧,一直有更新,但是两者不可在同一环境共存. 1 Python version 2. ...
- FreeBSD 系统的配置.
SSH 配置 vi /etc/ssh/sshd_config 修改下面的项目 RSAAuthentication yes PermitRootLogin yes PermitEmptypassword ...
- location下的属性集锦
location.protocol="http:"//即,协议 location.hostname="zhidao.baidu.com"//即,主机域名