【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计
Description
在得知了自己农场的完整地图后(地图形式如前三题所述),约翰又有了新的问题.他提供
一个整数K(1≤K≤109),希望你输出有多少对农场之间的距离是不超过K的.
Input
第1到I+M行:与前三题相同;
第M+2行:一个整数K.
Output
农场之间的距离不超过K的对数.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
10
Sample Output
5
有五对道路之间的距离小于10
1-4,距离为3
4-7,距离为2
1-7,距离为5
3-5,距离为7
3-6,距离为9
Solution
点分治
与【刷题】BZOJ 1468 Tree一模一样
题目里虽然是单向的,但是问的是距离,跟方向没关系,直接双向边建上
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=40000+10,inf=0x3f3f3f3f;
int n,k,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],deep[MAXN],cnt,Msonsize[MAXN],size[MAXN],d[MAXN],root,finish[MAXN],m;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
to[++e]=y;
nex[e]=beg[x];
beg[x]=e;
w[e]=z;
}
inline void getroot(int x,int f,int ntotal)
{
Msonsize[x]=0;size[x]=1;
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f||finish[to[i]])continue;
else
{
getroot(to[i],x,ntotal);
size[x]+=size[to[i]];
chkmax(Msonsize[x],size[to[i]]);
}
chkmax(Msonsize[x],ntotal-size[x]);
if(Msonsize[x]<Msonsize[root])root=x;
}
inline void getdeep(int x,int f)
{
deep[++cnt]=d[x];
for(register int i=beg[x];i;i=nex[i])
if(to[i]==f||finish[to[i]])continue;
else d[to[i]]=d[x]+w[i],getdeep(to[i],x);
}
inline int calc(int x,int st)
{
d[x]=st;cnt=0;
getdeep(x,0);
std::sort(deep+1,deep+cnt+1);
int l=1,r=cnt,ans=0;
while(l<r)
{
if(deep[l]+deep[r]<=k)ans+=r-l,l++;
else r--;
}
return ans;
}
inline int solve(int x)
{
int res=calc(x,0);
finish[x]=1;
for(register int i=beg[x];i;i=nex[i])
if(!finish[to[i]])
{
res-=calc(to[i],w[i]);
root=0;
getroot(to[i],x,size[to[i]]);
res+=solve(root);
}
return res;
}
int main()
{
read(n);read(m);
for(register int i=1;i<=m;++i)
{
int u,v,w;
char s;
read(u);read(v);read(w);std::cin>>s;
insert(u,v,w);insert(v,u,w);
}
read(k);
Msonsize[0]=inf;root=0;
getroot(1,0,n);
write(solve(root),'\n');
return 0;
}
【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计的更多相关文章
- BZOJ 3365: [Usaco2004 Feb]Distance Statistics 路程统计
Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献 ...
- bzoj 3365 [Usaco2004 Feb]Distance Statistics 路程统计(点分治,单调)
[题意] 求树上长度不超过k的点对数目. [思路] 和 Tree 一样一样的. 就是最后统计的时候别忘把根加上. [代码] #include<set> #include<cmath& ...
- bzoj 3365: [Usaco2004 Feb]Distance Statistics 路程统计【容斥原理+点分治】
统计在一个root下的两个子树,每个子树都和前面的运算一下再加进去对于这种需要排序的运算很麻烦,所以考虑先不去同子树内点对的算出合法点对个数,然后减去每一棵子树内的合法点对(它们实际上是不合法的,相当 ...
- BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治
BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治 Description 在得知了自己农 ...
- BZOJ1468: Tree & BZOJ3365: [Usaco2004 Feb]Distance Statistics 路程统计
[传送门:BZOJ1468&BZOJ3365] 简要题意: 给出一棵n个点的树,和每条边的边权,求出有多少个点对的距离<=k 题解: 点分治模板题 点分治的主要步骤: 1.首先选取一个点 ...
- BZOJ 3364: [Usaco2004 Feb]Distance Queries 距离咨询
Description 一棵树,询问两点间距离. Sol 倍增. 方向没用. 没有然后了. Code /************************************************ ...
- BZOJ 3367: [Usaco2004 Feb]The Big Game 球赛( dp )
dp(i)表示前i个人最少坐多少辆车, dp(i) = min(dp(j) + 1, dp(i)) (0 <= j < i 且 (i, j]的人能坐在一辆车上) 时间复杂度O(n²) -- ...
- LCA【bzoj3364】 [Usaco2004 Feb]Distance Queries 距离咨询
Description 奶牛们拒绝跑马拉松,因为她们悠闲的生活无法承受约翰选择的如此长的赛道.因此约翰决心找一条更合理的赛道,他打算咨询你.此题的地图形式与前两题相同.但读入地图之后,会有K个问题. ...
- BZOJ 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松
Description 给你一个图,两个点至多有一条路径,求最长的一条路径. \(n \leqslant 4\times 10^4\) Sol DFS?DP? 这就是一棵树,方向什么的都没用... 然 ...
随机推荐
- Python+MySQL开发医院网上预约系统(课程设计)二
---恢复内容开始--- 1:报错 1.1.创建表时报错 CREATE TABLE Admin ( A_ID VARCHAR(20) NOT NULL AUTO_INCREMENT, p ...
- Siki_Unity_2-7_Stealth秘密行动
Unity 2-7 Stealth秘密行动 Abstract:向量运算:Animation动画:Navigation寻路系统:Mecanim动画系统 任务1&2&3:游戏介绍 & ...
- 【python 2.7】python读取json数据存入MySQL
同上一篇,只是适配 CentOS+ python 2.7 #python 2.7 # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import json im ...
- java学习笔记-01.对象入门
1.面向对象编程简称是OOP. 2.继承是通过 extends关键字实现的,接口是通过implements关键字实现的. 3.public:意味着后续的定义任何人均可使用. private:意味着除了 ...
- Python基础灬dict&set
字典dict 字典使用键-值(key-value)存储,具有极快的查找速度. dict基本操作 取值 a_dict = {'name': 'jack', 'age': 18} print(a_dict ...
- Python数据挖掘——数据概述
Python数据挖掘——数据概述 数据集由数据对象组成: 数据的基本统计描述 中心趋势度量 均值 中位数 众数 中列数 数据集的最大值和最小值的平均 度量数据分布 极差 最大值与最小值的差 四分位数 ...
- nordic-mesh中应用的代码实现
nordic-mesh中应用的代码实现 Nordic-Mesh遵循SIG-Mesh-Profile中的mesh定义,实现了element.model等概念. 一个应用中包含一个或多个element,e ...
- SQL判断是否存在
判断数据库是否存在 ifexists(select*frommaster..sysdatabaseswherename=N’库名’) print’exists’ else print’notexist ...
- 联邦快递 IE和IP的区别 Fedex IE VS Fedex IP
什么是FedEx IP? FedEx IP指的是联邦快递优先服务,时效比较快些,相对来说价格也比普通的高一些. 什么是FedEx IE? FedEx IE指的是联邦快递经济服务,时效与FedEx IP ...
- [C++] Solve "Cannot run program "gdb": Unknown reason" error
In Mac OSX, The Issue Image: 1. Build the project on Eclipse successfully. 2. Run gdb on command lin ...