POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)
题目大意:(同poj1741,刷一赠一系列)
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 40010
#define INF 0x3f3f3f3f
using namespace std; int points,edges,k;
int head[MAX],total;
int next[MAX << 1],length[MAX << 1],aim[MAX << 1]; bool v[MAX];
int cnt[MAX];
int _size,size[MAX],root,_total;
int dis[MAX],p; char s[10]; inline void Add(int x,int y,int len); void Work(int x);
void GetRoot(int x,int last);
inline int Count(int x,int len);
void GetDis(int x,int last,int len); int main()
{
cin >> points >> edges;
for(int x,y,z,i = 1;i <= edges; ++i) {
scanf("%d%d%d%s",&x,&y,&z,s);
Add(x,y,z),Add(y,x,z);
}
cin >> k;
Work(1);
int ans = 0;
for(int i = 1;i <= points; ++i)
ans += cnt[i];
cout << ans << endl;
return 0;
} inline void Add(int x,int y,int len)
{
next[++total] = head[x];
aim[total] = y;
length[total] = len;
head[x] = total;
} void Work(int x)
{
_total = size[x] ? size[x]:points;
_size = INF;
GetRoot(x,0);
x = root;
v[x] = true;
cnt[x] = Count(x,0);
for(int i = head[x];i;i = next[i]) {
if(v[aim[i]]) continue;
cnt[x] -= Count(aim[i],length[i]);
Work(aim[i]);
}
} void GetRoot(int x,int last)
{
size[x] = 1;
int max_size = 0;
for(int i = head[x];i;i = next[i]) {
if(v[aim[i]] || aim[i] == last) continue;
GetRoot(aim[i],x);
size[x] += size[aim[i]];
max_size = max(max_size,size[aim[i]]);
}
max_size = max(max_size,_total - size[x]);
if(_size > max_size)
_size = max_size,root = x;
} inline int Count(int x,int len)
{
p = 0;
GetDis(x,0,len);
sort(dis + 1,dis + p + 1);
int l = 1,r = p,re = 0;
while(l < r) {
if(dis[l] + dis[r] <= k)
re += (r - l),++l;
else --r;
}
return re;
} void GetDis(int x,int last,int len)
{
dis[++p] = len;
for(int i = head[x];i;i = next[i]) {
if(v[aim[i]] || aim[i] == last) continue;
GetDis(aim[i],x,len + length[i]);
}
}
POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)的更多相关文章
- [BZOJ 3365] Distance Statistics
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3365 [算法] 点分治 [代码] #include <algorithm> ...
- BZOJ 3365 Distance Statistics 点分治
这道题是一道点分治的题目,难度不大,可以拿来练手. 关键是对于找出来的重心的删除操作需要删掉这条边,这很重要. 还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz. 然后就A了 ...
- BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治
BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治 Description 在得知了自己农 ...
- POJ 1987 Distance Statistics 树分治
Distance Statistics Description Frustrated at the number of distance queries required to find a ...
- bzoj 3365 [Usaco2004 Feb]Distance Statistics 路程统计(点分治,单调)
[题意] 求树上长度不超过k的点对数目. [思路] 和 Tree 一样一样的. 就是最后统计的时候别忘把根加上. [代码] #include<set> #include<cmath& ...
- BZOJ 4016 [FJOI2014]最短路径树问题 (贪心+点分治)
题目大意:略 传送门 硬是把两个题拼到了一起= = $dijkstra$搜出单源最短路,然后$dfs$建树,如果$dis_{v}=dis_{u}+e.val$,说明这条边在最短路图内,然后像$NOIP ...
- POJ 1987 Distance Statistics
http://poj.org/problem?id=1987 题意:给一棵树,求树上有多少对节点满足距离<=K 思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉 ...
- poj 2104 K-th Number(主席树,详细有用)
poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...
- 【BZOJ】3319: 黑白树
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...
随机推荐
- (转)C#之玩转反射
前言 之所以要写这篇关于C#反射的随笔,起因有两个: 第一个是自己开发的网站需要用到 其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...
- FineUI上传控件
文件上传 现在就简单多了,并且也漂亮多了,参考这个示例. 1: <ext:SimpleForm ID="SimpleForm1" BodyPadding="5px& ...
- SQL Server Profile:使用方法和指标说明
SQL Server Profiler的中文意思是SQL Server事件探查,一个Sql的监视工具,可以具体到每一行Sql语句,每一次操作,和每一次的连接.感觉这个工具的作用还是很大的,给大家分享一 ...
- android内存优化之图片压缩和缓存
由于手机内存的限制和网络流量的费用现在,我们在加载图片的时候,必须要做好图片的压缩和缓存. 图片缓存机制一般有2种,软引用和内存缓存技术. 1.压缩图片:压缩图片要既不能模糊,也不能拉伸图片. 图片操 ...
- hdu 1258 DFS
I - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bi ...
- Introduction to the POM
原文:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html Introduction to the POM ...
- The Lead Game Add problem to Todo list Problem code: TLG
'''def count_lead(first, second): if first > second: return 1, first - second elif first == secon ...
- NAS4Free 安装配置(四)配置硬盘
配置硬盘 磁盘管理 进入磁盘管理界面:Disks|Management 磁盘管理 点击Import disks导入磁盘 把第4个4G的DOM盘删掉,因为我们不在这个盘上存放数据 点Apply chan ...
- HTML中select标签单选多选详解
select 元素可创建单选或多选菜单.当提交表单时,浏览器会提交选定的项目,或者收集用逗号分隔的多个选项,将其合成一个单独的参数列表,并且在将 <select> 表单数据提交给服务器时包 ...
- iOS开发:详解Objective-C runTime
Objective-C总Runtime的那点事儿(一)消息机制 最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎 ...