【poj1987】 Distance Statistics
http://poj.org/problem?id=1987 (题目链接)
题意
给出一棵树,求树上距离不超过K的点对个数。
Solution
点分治,同poj1741。
代码
// poj1987
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define MOD 100000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=40010;
struct edge {int to,next,w;}e[maxn<<1];
int head[maxn],deep[maxn],d[maxn],size[maxn],f[maxn],vis[maxn];
int n,m,K,cnt,rt,sum,ans; void link(int u,int v,int w) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;e[cnt].w=w;
}
void calroot(int x,int fa) {
size[x]=1;f[x]=0;
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to] && e[i].to!=fa) {
calroot(e[i].to,x);
size[x]+=size[e[i].to];
f[x]=max(f[x],size[e[i].to]);
}
f[x]=max(f[x],sum-size[x]);
if (f[x]<f[rt]) rt=x;
}
void caldeep(int x,int fa) {
deep[++deep[0]]=d[x];
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to] && e[i].to!=fa) {
d[e[i].to]=d[x]+e[i].w;
caldeep(e[i].to,x);
}
}
int cal(int x,int now) {
d[x]=now;deep[0]=0;
caldeep(x,0);
sort(deep+1,deep+deep[0]+1);
int t=0;
for (int l=1,r=deep[0];l<r;) {
if (deep[l]+deep[r]<=K) t+=r-l,l++;
else r--;
}
return t;
}
void solve(int x) {
vis[x]=1;
ans+=cal(x,0);
for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) ans-=cal(e[i].to,e[i].w);
for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) {
rt=0;sum=size[e[i].to];
calroot(e[i].to,0);
solve(rt);
}
}
int main() {
scanf("%d%d",&n,&m);
char ch;
for (int u,v,w,i=1;i<=m;i++) {
scanf("%d%d%d %c",&u,&v,&w,&ch);
if (u==v) continue;
link(u,v,w);
}
scanf("%d",&K);
rt=0;sum=n;f[0]=inf;
calroot(1,0);
solve(rt);
printf("%d",ans);
return 0;
}
【poj1987】 Distance Statistics的更多相关文章
- 【树形dp】【CF161D】distance on a tree + 【P1352】没有上司的舞会
T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了- ...
- 【leetcode】1093. Statistics from a Large Sample
题目如下: We sampled integers between 0 and 255, and stored the results in an array count: count[k] is ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- 【BZOJ2790】[Poi2012]Distance 筛素数+调和级数
[BZOJ2790][Poi2012]Distance Description 对于两个正整数a.b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p, 如果选择变成a/p ...
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- 论文阅读笔记(二)【IJCAI2016】:Video-Based Person Re-Identification by Simultaneously Learning Intra-Video and Inter-Video Distance Metrics
摘要 (1)方法: 面对不同行人视频之间和同一个行人视频内部的变化,提出视频间和视频内距离同时学习方法(SI2DL). (2)模型: 视频内(intra-vedio)距离矩阵:使得同一个视频更紧凑: ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Codevs 1860 最大数 string大法好,STL万岁。。
题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. 第二行n个正整数,空格隔开 ...
- [转]PHP 下使用 ZeroMQ 和 protobuf
FROM : http://www.68idc.cn/help/makewebs/php/20150118175432.html 前言 这个记录总的来说分两部分: 搭建环境. 简单使用教程. 搭建环境 ...
- opencv 中对一个像素的rgb值或像素值进行操作的几个常用小办法【转】
You can access the Image pixels in many ways:1. One using the Inbuilt macro2. One using the pointer ...
- JavaScript的一些知识碎片(2)-反射-全局变量-回调
JavaScript中的反射:编程语言中的反射原理都一样,就是通过操作metadata(描述语言的语言)来完成一些不具备反射功能的语言很难实现的功能.在静态语言中,反射是一个高大上的东西,比如在运行时 ...
- SQL基础之数据库
1.基础概念 首先要强调一点,就是我们的数据库是由数据库系统来管理的,我们登入数据库并在其上进行操作时最终均要通过数据库系统来完成.可以理解成在数据库上进行操作的是客户端,数据库系统是服务端.一个数据 ...
- des解密不完整,前面几位是乱码的解决办法
在工作中遇到的Des解密问题,第三方发来的数据需要我们进行des解密,但是解密的结果前几位始终是乱码.废了半天劲,终于找到了问题所在. 下面先介绍一下des,了解des的同学可以直接看下面的解决办法. ...
- 使用spring boot和thrift、zookeeper建立微服务
Spring cloud适应于云端服务,也适用于企业信息化SOA建设.spring boot也是restful微服务开发的利器.但对于内网服务,即服务与服务之间的调用,spring并没有去刻意封装,也 ...
- MC700 安装双系统
2011年买的MBP MC700给老婆用了一段时间后,老婆还不习惯不了Mac OS或是虚拟机,要求必须给安装windows,无奈时隔四年后,只能重新尝试在MC700上用bootcamp安装Window ...
- 关于Hellas和Greece
一直以来我就好奇,为什么希腊的中文名字“希腊”和英文名字”Greece”听起来都不像(就像“德国”不像“Germany”一样),而且,为什么在很多体育比赛中看到希腊运动员的衣服上都是“Hellas”, ...
- offsetleft、offsetTop、offsetParent的兼容性问题
先来看看offsetParent返回的是什么值 ele.offsetParent返回的是ele元素最近的并且是定位过(relative,absolute)的父元素,如果没有父元素或者是父元素中没有一个 ...