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行边描述管道,按照题目中写的输入 接下来是 ...
随机推荐
- Ng-model undefined in the controller
这个问题是我最近在项目中碰到的,暂时没找到原因,找到一个解决方法,还多请大神指教,在Stack Overflow找到解决方法: I am having some "problems" ...
- C++ 输出Cstring遇见的奇葩问题
先上代码 // webConteng.cpp : Defines the entry point for the console application. // #include "stda ...
- HaiHongOJ 1003 God Wang
题目连接:http://oj.haihongblog.com/problem.php?id=1003 线段树维护区间最小值,并且求解下标 #include <stdio.h> #inclu ...
- 004-Python字符串
Python 字符串(str) 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串.创建字符串很简单,只要为变量分配一个值即可. var1 = "H ...
- [iOS]C语言技术视频-13-指针变量练习三(结构体指针变量)
下载地址: 链接: http://pan.baidu.com/s/1bnx2xm3 密码: t4mj
- Windows Server 2012 在个人终端上使用的推荐设置
Windows Server 2012,也就是 Windows 8 的服务器版本,相对于 Windows 8 企业版而言,增强了作为服务器的功能,弱化了作为终端系统的功能. 目前微软官方提供了 Win ...
- 高橋君とカード / Tak and Cards
高橋君とカード / Tak and Cards Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 300 p ...
- 小米1S iptables禁止443端口
shell@android:/system/bin # ./iptables -A INPUT -p tcp --dport 443 -j DROP./iptables -A INPUT -p tcp ...
- Xcode7 免证书真机测试
Xcode很早就有个免证书测试,今天我自己也测试了一把,还是挺好用的,接下来,我就说一下我的大体过程: 注意:一定要让你的真机设备的系统版本和app的系统版本想对应,如果不对应就会出现一个很常见的问题 ...
- css3选择器总结--强大如jquery
最近发现,阿里的笔试考了许多css3的知识,像query media.box-flex等等.主要是移动浏览器的开发,让html5和css3如虎添翼,再也不用担心兼容了.so总结一下css3的选择器: ...