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 ...
随机推荐
- MySQL数据库怎么截取字符串?
函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...
- WCF:REST + Basic authentification + IIS
近期一个项目中用到Restful WCF提供服务,但是需要验证机制,网上搜刮了一些,都是太复杂.FQ找到了一篇不错的文章分享一下. 原地址连接:http://vgolovchenko.wordpres ...
- windows10下git一些问题
windows10下安装git 找不到ssh解决办法 解决办法是: 输入下列命令,一路回车 $ ssh-keygen -t rsa -C “邮箱地址” 若执行ssh-add /path/to/xxx. ...
- Python3 小工具-UDP扫描
from scapy.all import * import optparse import threading def scan(target,port): pkt=IP(dst=target)/U ...
- 适合初学者的嵌入式Linux计划
俗话说万事开头难,刚开始的时候,你是否根本就不知如何开始,上网查资料被一堆堆新名词搞的找不到北,去图书馆看书也是找不到方向?又是arm,又是linux,又是uboot头都大了,不知道自己究竟从哪里开始 ...
- C++读取文件统计单词个数及频率
1.Github链接 GitHub链接地址https://github.com/Zzwenm/PersonProject-C2 2.PSP表格 PSP2.1 Personal Software Pro ...
- GPS定位,经纬度附近地点查询–C#实现方法
摘要:目前的工作是需要手机查找附近N米以内的商户,功能如下图数据库中记录了商家在百度标注的经纬度(如:116.412007,39.947545),最初想法以圆心点为中心点,对半径做 ...
- hadoop下安装mysql
http://www.cnblogs.com/zhuyp1015/p/3561470.html 第一步:先把这个文件放入到linux环境下桌面. 接着编写脚本:sudo apt-get u ...
- phpcmsv9 同时调用多个栏目的文章标签
V9版本默认好像没有多栏目调用的标签,例如我用{pc:content action="lists" catid ="6,7,8,9,10" num=" ...
- Linux环境PHP5.6升级7.1.8
PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导致crash了, 那么整个服务就挂 ...