【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 ...
随机推荐
- vijos P1448 校门外的树
描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的--如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:\(K=1\),读入\(l, ...
- 【转】【MySQL】mysql 通过bin-log恢复数据方法详解
mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...
- QT 数据库编程一
QT如果要进行网络编程首先需要在.pro中添加如下代码:QT += network //logindlg.h #ifndef LOGINDLG_H #define LOGINDLG_H #includ ...
- HFSS学习
关于边界条件和端口激励的设置,是HFSS应用和学习的重点和难点:“边界条件决定场”,正确地理解和使用边界条件是正确使用HFSS仿真分析电磁问题的前提:HFSS中定义了多种边界条件,大家在学习过程中必须 ...
- list使用例子(转)
例子: 在vs2010中创建一个winform的解决方案,然后定义一个类Person,Person.cs 的代码如下: using System;using System.Collections.Ge ...
- 简单高效的nodejs爬虫模型
这篇文章讲解一下yunshare项目的爬虫模型. 使用nodejs开发爬虫很简单,不需要类似python的scrapy这样的爬虫框架,只需要用request或者superagent这样的http库就能 ...
- 4.5你太黑了,不带这么玩TypeForwardedTo的
话说最近好不容易把framework 4.0的metadata信息都能全部抽出了,结果换4.5挂了...framework那帮人在4.5里面用了些什么诡异的玩意? 结果一看4.5的部分field用了t ...
- 记一次在Eclipse中用Axis生成webservice服务端的过程中出现的问题
问题一. Unable to find config file. Creating new servlet engine config file: /WEB-INF/server-config.ws ...
- MVC架构设计——EF-Code First
详情参考:http://www.cnblogs.com/guomingfeng/archive/2013/05/28/mvc-ef-repository.html
- 【摘抄】将xml注释文档生成网页
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath(&quo ...