【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 ...
随机推荐
- Java集合系列:-----------03ArrayList源码分析
上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解ArrayLi ...
- Visual Studio 2013编辑HTML文件无设计视图的解决方案
在Visual Studio 2013中编辑HTML文件,会发现没有设计视图. 解决方法:点击Visual Studio 2013的”工具“菜单,再点击”选项“—>文本编辑器—>文件扩展名 ...
- [转]Linux后台进程管理利器:supervisor
FROM : http://www.liaoxuefeng.com/article/0013738926914703df5e93589a14c19807f0e285194fe84000 Linux后台 ...
- ImageMagick常用指令详解
Imagemagick常用指令 (ImageMagick--蓝天白云) (ImageMagick官网) (其他比较有价值的IM参考) (图片自动旋转的前端实现方案) convert 转换图像格式和大小 ...
- windows7下启动mysql服务出现服务名无效
出现提示: WIN 7 cmd命令行下,net start mysql,出现 服务名无效提示: 问题原因: mysql服务没有安装. 解决办法: 在 mysql bin目录下 以管理员的权限 执行 m ...
- Oracle基础语法
--表create table tb_myTable( mname vardhar2(30), pwd varchar2(30)); --存储过程create or replace procedure ...
- DbEntry在Vs2012里的配置
dbentry官方的版本还不支持vs2012,要再vs2012中使用,必须做下调整 1:新建类库项目,然后添加dbentry 的dll引用. 2:在建好的类库项目中.csproj 新添加了类库项目后, ...
- Python3.5 + django1.8.5 安装”import pymysql pymysql.install_as_MySQLdb()”的解决方法
最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到"被解放的姜戈"时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: ...
- Win2008上.NET4.0部署出错HTTP 错误 500.21 - Internal Server Error的解决方法
原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装 解决:开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行 ...
- JS 获取上一层目录
派生到我的代码片 <script type="text/javascript"> //返回当前工作目录 function GetCurrDir(){ var pathN ...