Distance Statistics
 
 

Description

Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K <= 1,000,000,000) and wants to know how many pairs of farms lie at a distance at most K from each other (distance is measured in terms of the length of road required to travel from one farm to another). Please only count pairs of distinct farms (i.e. do not count pairs such as (farm #5, farm #5) in your answer). 
 

Input

* Lines 1 ..M+1: Same input format as in "Navigation Nightmare"

* Line M+2: A single integer, K.

 

Output

* Line 1: The number of pairs of farms that are at a distance of at most K from each-other. 
 

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

Hint

There are 5 roads with length smaller or equal than 10, namely 1-4 (3), 4-7 (2), 1-7 (5), 3-5 (7) and 3-6 (9). 
 

题解:

  POJ 1741

  http://www.cnblogs.com/zxhl/p/5692688.html

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 4e4+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int ans, n,m,root , t = ,K,siz[N],head[N],f[N],deep[N],d[N],allnode,vis[N];
struct edg{int to,next,v,w;}e[N * ];
void add(int u,int v,int w) {e[t].to=v;e[t].v=w;e[t].next=head[u];head[u]=t++;} void getroot(int x,int fa) {
siz[x] = ;
f[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
getroot(to,x);
siz[x] += siz[to];
f[x] = max(f[x] , siz[to]);
}
f[x] = max(f[x] , allnode - siz[x]);
if(f[x] < f[root]) root = x;
}
void getdeep(int x,int fa) {
if(d[x] <= K) deep[++deep[]]=d[x];
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
d[to] = d[x] + e[i].v;
getdeep(to,x);
}
}
int cal(int x,int now) {
d[x]=now;deep[] = ;
getdeep(x,);
sort(deep+,deep+deep[]+);
int all = ;
for(int l=,r=deep[];l<r;) {
if(deep[l]+deep[r] <= K) {all+=r-l;l++;}
else r--;
}
return all;
}
void work(int x) {
ans+=cal(x,);
vis[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
ans-=cal(to,e[i].v);
allnode = siz[to];
root = ;
getroot(to,root);
work(root);
}
}
void init()
{
memset(head,,sizeof(head));
t = ;
ans = root = ;
memset(vis,,sizeof(vis));
}
int main()
{
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=;i<n;i++) {
int a,b,c;char ch[];
scanf("%d%d%d%s",&a,&b,&c,ch);
add(a,b,c) , add(b,a,c);
}
scanf("%d",&K);
allnode=n;f[]=inf;
getroot(,);
work(root);
printf("%d\n",ans);
} }

POJ 1987 Distance Statistics 树分治的更多相关文章

  1. POJ 1987 Distance Statistics(树的点分治)

      转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 上场CF的C题是一个树的分治... 今天刚好又 ...

  2. POJ 1987 Distance Statistics

    http://poj.org/problem?id=1987 题意:给一棵树,求树上有多少对节点满足距离<=K 思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉 ...

  3. POJ 1741 Tree【树分治】

    第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重 ...

  4. POJ 1741 Tree ——(树分治)

    思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述. 复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排 ...

  5. POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)

    题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...

  6. POJ 1741 Tree (树分治入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8554   Accepted: 2545 Description ...

  7. poj 2114 Boatherds (树分治)

    链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...

  8. BZOJ 3365 Distance Statistics 点分治

    这道题是一道点分治的题目,难度不大,可以拿来练手. 关键是对于找出来的重心的删除操作需要删掉这条边,这很重要. 还有每次找重心的时候,不但要考虑他的子节点的siz,还要考虑父节点的siz. 然后就A了 ...

  9. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

随机推荐

  1. Shell 读取文本内容

    在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. ...

  2. HBase独立集群部署

    HBase是分布式.面向列式存储的开源数据库,来源于Google的论文BigTable,HBase运行于Hadoop平台之上,不同于一般的关系数据库,是一个适合非结构化数据存储的分布式数据库 安装Hb ...

  3. Delphi XE5 android 获取网络状态

    unit Androidapi.JNI.Network; interface function IsConnected: Boolean; function IsWiFiConnected: Bool ...

  4. ABAP 自建透明表维护

    *&---------------------------------------------------------------------* *& Report  ZMMR011 ...

  5. iOS SHA1加密实现方法

    使用方法 先导入头文件 #import "SHA1.h" //SHA1测试 NSString* sh1=[SHA1 getSha1String:"]; NSLog(@&q ...

  6. php 获取IP

    <?php echo 'your ip is :'; if (@$_SERVER["HTTP_X_FORWARDED_FOR"]) $ip = $_SERVER[" ...

  7. Effective C++ -----条款52:写了placement new 也要写 placement delete

    当你写一个placement operator new ,请确定也写出了对应的placement operator delete.如果没有这样做,你的程序可能会发生隐微而时断时续的内存泄漏. 当你声明 ...

  8. 如何让两个 并列的div高度相等

    哪个div Height值大,就将其值赋给Height值小的div,从而使2个div高度始终保持一致. function $(id){ return document.getElementById(i ...

  9. ORACLE 远程导入导出数据库

      Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. ...

  10. jquery中dom元素的attr和prop方法的理解

    一.背景 在编写使用高版本[ jQuery 1.6 开始新增了一个方法 prop()]的jquery插件进行编写js代码的时候,经常不知道dom元素的attr和prop方法到底有什么区别?各自有什么应 ...