bzoj 1468 Tree 点分
Tree
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1972 Solved: 1101
[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
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath> #define N 40007
#define inf 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,k,ans,id,total;
int cnt,hed[N],rea[N<<],nxt[N<<],val[N<<];
int tot,num[N],siz[N],f[N];
bool vis[N]; void add(int u,int v,int z)
{
nxt[++cnt]=hed[u];
hed[u]=cnt;
rea[cnt]=v;
val[cnt]=z;
}
void get_heart(int u,int fa)
{
siz[u]=,f[u]=;
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i];
if (v==fa||vis[v]) continue;
get_heart(v,u);
siz[u]+=siz[v];
f[u]=max(f[u],siz[v]);
}
f[u]=max(f[u],total-f[u]);
if (f[u]<f[id]) id=u;
}
void dfs_dep(int u,int now,int fa)
{
num[++tot]=now;
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i],fee=val[i];
if (v==fa||vis[v]) continue;
dfs_dep(v,now+fee,u);
}
}
int calc(int u,int now)
{
tot=;int res=;
dfs_dep(u,now,);
sort(num+,num+tot+);
int l=,r=tot;
while(l<r)
{
if (num[l]+num[r]<=k) res+=r-l,l++;
else r--;
}
return res;
}
void solve(int u)
{
ans+=calc(u,);
vis[u]=true;
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i],fee=val[i];
if (vis[v]) continue;
ans-=calc(v,fee);
total=siz[v],id=;
get_heart(v,u);
solve(v);
}
}
int main()
{
memset(hed,-,sizeof(hed));
n=read();
for (int i=;i<n;i++)
{
int x=read(),y=read(),z=read();
add(x,y,z);
add(y,x,z);
}
k=read();
total=n,f[]=inf;
get_heart(,-);
solve(id);
printf("%d\n",ans);
}
bzoj 1468 Tree 点分的更多相关文章
- bzoj 1468 Tree(点分治模板)
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1527 Solved: 818[Submit][Status][Discuss] ...
- BZOJ 1468: Tree
Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...
- 【刷题】BZOJ 1468 Tree
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
随机推荐
- Apache——访问控制
Order 指定执行允许访问规则和拒绝访问规则 Deny 定义拒绝访问列表 Allow 定义允许访问列表 Order allow,deny 先执行允许,再执行拒绝 Order deny,allow ...
- CTC (Connectionist Temporal Classification) 算法原理
(原创文章,转载请注明出处哦~) 简单介绍CTC算法 CTC是序列标注问题中的一种损失函数. 传统序列标注算法需要每一时刻输入与输出符号完全对齐.而CTC扩展了标签集合,添加空元素. 在使用扩展标签集 ...
- wamp下安装https 实现 ssl 协议,主要是编写小程序通讯
也不知道腾讯为啥要这个限制,是想卖他的服务器资源么 简单几句话 1 wamp3.0.X的版本不行,我折腾了一天半,放弃了,换成wamp2.5 一次通过 2 证书 去腾讯云申请,单独域名的可以申请免费的 ...
- LVS+Keepalive+Nginx实现负载均衡
本文参考:http://blog.csdn.net/yinwenjie/article/details/47211551 简单粗暴写一下,做备忘,刚刚搭好没做优化呢,后期补充 一.机器准备 LVS-M ...
- 迭代器类型:iterator & const_iterator
vector<int> ivec{1, 3, 4, 1, 3, 4}; vector<int>::iterator iter; // iter能读写vector<int& ...
- Log Files
Description Nikolay has decided to become the best programmer in the world! Now he regularly takes p ...
- 系统常量对话框QT实现
1.运行结果: 2.代码 main.cpp #include "constantdiag.h" #include <QtWidgets/QApplication> in ...
- 【APS.NET Core】- Json配置文件的读取
在项目目录下有个 appsettings.json ,我们先来操作这个文件.在appsettings.json中添加以下内容: { "Logging": { "LogLe ...
- 全面了解 Nginx 到底能做什么
来源:https://www.jianshu.com/p/8bf73d1a758c 前言 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可 ...
- C# 压缩组件介绍与入门
1.前言 作为吉日嘎拉权限管理系统最早的一批学习版用户,学了不少东西,在群里面也结识了很多朋友,更重要的是闲余时间,大家都发布很多可靠的外包工作.这次也是由于吉日嘎拉发布了一个有关“压缩文件损坏检测” ...