【刷题】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? 这就是一棵树,方向什么的都没用... 然 ...
随机推荐
- iOS 关于内购
最近项目的第三方支付导致项目被拒,记录一下关于内购 #import <StoreKit/StoreKit.h> //沙盒测试环境验证 #define SANDBOX @"http ...
- JAVA高级之路----JAVA多线程
介绍 这段时间一直在学习和整理一些通往java高级程序猿必备的知识点,有些是工作中必须要知道的,有些是面试必须要知道的, 但是不管怎么样,学习了就不会有坏处,不可能全部记得住,最起码得雁过留痕,知识不 ...
- Python小白学习之文件内建函数
文件内建函数: 2018-10-24 23:40:02 简单介绍: open()打开文件 read()读取文件(其实是输入文件里的内容到read函数,类似于get(url),所以下面的图片备注的是 ...
- Kubernetes v1.10----部署kubernetes-dashboard v1.83
Kubernetes v1.10----部署kubernetes-dashboard v1.83 1.下载 kubernetes-dashboard yaml文件 #因为文件中的image指定的是谷 ...
- clone中的浅复制和深复制
clone:用于两个对象有相同的内容时,进行复制操作. 提示:Java中要想自定义类的对象可以被复制,自定义类就必须实现Cloneable中的clone()方法. 浅复制:另一个对象用clone()方 ...
- AutoCAD2015激活码和密钥
CAD2015序列号和密钥 序列号: 666-69696969 产品密钥: 001G1 序列号: 356-72378422 产品密钥: 206G1 安装说明 ...
- Arctic Network POJ 2349 (最小生成树思想)
Description The Department of National Defence (DND) wishes to connect several northern outposts by ...
- 复利计算器app发布
复利计算器app发布 抱歉:由于无法实现服务端的持续开启,发布的app仅为简单的单机版,暂时舍弃了c/s版本的一些功能,如:投资动态管理功能. 应用详情博客:请点击这里 apk下载地址1(百度手机助手 ...
- 第二次c艹作业
1,c语言实现方法:按照电梯运行方式,改变被定义为全局变量的结构体的数值. c艹实现方法:用类来存放电梯的属性,整个过程都是对类操作. 两者不同:c语言是面向过程的,整个函数里都要对电梯的参数进行修改 ...
- HDU 5172 GTY's gay friends 线段树+前缀和+全排列
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...