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. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  2. linux skill

    linux console终端乱码解决 1.console终端乱码 在/etc/profile文件的最后一行添加如下内容: export LC_ALL="zh_CN.GB18030" ...

  3. Bitmap vs 2Bitmap的实现

    [本文链接] http://www.cnblogs.com/hellogiser/p/bitmap-vs-2bitmap.html [题目] 在2.5亿个整数找出不重复的整数,内存不足以容纳着2.5亿 ...

  4. SQL Update 巧用

    JOIN 样本 ********************************** Update 结存 set 结存.现有库存=c.入仓数-b.出仓数量 from 结存 a )) 入仓数 from ...

  5. SQL Server的“错误:9004”

    客户的一台现场采集电脑崩溃,无法启动.用PE引导后,将MDF和LDF数据复制出来,往正常的数据库附加时,报如下错误: 处理数据库 'databasename' 的日志时出错.如果可能,请从备份还原.如 ...

  6. JSON数据格式

    JSON 数据格式 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人 ...

  7. unbutu下搭建FTP服务

    安装 apt-get install vsftpd 启动 service vsftpd start 第一次连接的时候出了点问题,报了一个 login incorrect 530的连接错误 然后百度了一 ...

  8. struts2截取字符串

    <struts:if test="null!=pushAd&&pushAd.length()>14"> <struts:property v ...

  9. 9.12/ css3拓展、js基础语法、程序基本知识、数据类型、运算符表达方式、语句知识点

    css3拓展: <display:none>  将某个元素隐藏       <visibility:hidden>  也是将某个元素隐藏 <display:block&g ...

  10. JqueryAjaxFormData文件异步上传

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...