P3565 [POI2014]HOT-Hotels
题目描述
There are nn towns in Byteotia, connected with only n-1n−1 roads.
Each road directly links two towns.
All the roads have the same length and are two way.
It is known that every town can be reached from every other town via a route consisting of one or more (direct-link) roads.
In other words, the road network forms a tree.
Byteasar, the king of Byteotia, wants three luxury hotels erected to attract tourists from all over the world.
The king desires that the hotels be in different towns and at the same distance one from each other.
Help the king out by writing a program that determines the number of possible locations of the hotel triplet in Byteotia.
输入输出格式
输入格式:
The first line of the standard input contains a single integer nn (1\le n\le 5\ 0001≤n≤5 000), the number of towns in Byteotia.
The towns are numbered from 11 to nn.
The Byteotian road network is then described in n-1n−1 lines.
Each line contains two integers aa and bb (1\le a\le b\le n1≤a≤b≤n) , separated by a single space, that indicate there is a direct road between the towns aa and bb.
输出格式:
The first and only line of the standard output should contain a single integer equal to the number of possible placements of the hotels.
输入输出样例
输入样例#1: 复制
7
1 2
5 7
2 5
2 3
5 6
4 5
输出样例#1: 复制
5
给定一棵树,求有多少三点间距离两两相等
\(asuldb\)怒嘲这道题和给定一棵树求有多少点一样简单
其实三个点间距离两两相等当且仅当这三个点到同一个点距离相等且路径不重合,
我们处理出到距离每个点每个距离的点的个数,然后排列组合
每次换根,那么三个点一等在不同子树,所有情况-三个点在同一子树情况-两个点在同一子树情况即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define max(a,b) ((a)>(b)? (a):(b))
#define min(a,b) ((a)<(b)? (a):(b))
using namespace std;
int dp[5011][5011],i,j,k,m,n,d[5011],ver[10011],nex[10011],head[10111],cnt,de[10011],x,y;
LL ans;
void add(int x,int y)
{
cnt+=1;
ver[cnt]=y; nex[cnt]=head[x]; head[x]=cnt;
}
void dfs1(int now,int fa,int f)
{
int r[10001];
for(int i=1;i<=f;i++) r[i]=d[i];
for(int i=1;i<=max(f,de[now]);i++)
{
if(d[i]>=3) ans-=(LL)d[i]*(d[i]-1)*(d[i]-2)/6;
dp[now][i]+=d[i];
if(dp[now][i]>=3) ans+=(LL)dp[now][i]*(dp[now][i]-1)*(dp[now][i]-2)/6;
}
for(int i=head[now];i;i=nex[i])
{
int t=ver[i];
if(t==fa) continue;
for(int j=1;j<=de[t];j++)
{
if(dp[t][j]>=3) ans-=(LL)dp[t][j]*(dp[t][j]-1)*(dp[t][j]-2)/6;
if(dp[now][j+1]>=3 && dp[t][j]>=2) ans-=(LL)dp[t][j]*(dp[t][j]-1)*(dp[now][j+1]-dp[t][j])/2;
}
}
for(int i=1;i<=max(f,de[now]);i++)
if(d[i]>=2 && dp[now][i]>=3) ans-=(LL)d[i]*(d[i]-1)*(dp[now][i]-d[i])/2;
for(int i=head[now];i;i=nex[i])
{
int t=ver[i];
int g=max(f+1,de[now]+1);
if(t==fa) continue;
for(int j=g;j>=2;j--)
d[j]=dp[now][j-1]-dp[t][j-2];
d[1]=1;
dfs1(t,now,g);
for(int j=1;j<=f;j++) d[j]=r[j];
}
}
void dfs(int now,int fa)
{
de[now]=1;
dp[now][0]=1;
for(int i=head[now];i;i=nex[i])
{
int t=ver[i];
if(t==fa) continue;
dfs(t,now);
de[now]=max(de[now],de[t]+1);
for(int j=0;j<=de[t];j++) dp[now][j+1]+=dp[t][j];
}
}
int main()
{
scanf("%d",&n);
for(i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(1,0);
dfs1(1,0,1);
printf("%lld",ans);
}
P3565 [POI2014]HOT-Hotels的更多相关文章
- luogu P3565 [POI2014]HOT-Hotels
传送门 无脑暴力+O2=AC 题目要统计距离两两相等的三个点的组数,这三个点之间显然有一个点,并且这三个点到这个点的距离都相同.所以枚举中间这个点作为根,然后bfs整棵树,对于每一层,把以根的某个儿子 ...
- ZJOI2019一轮停课刷题记录
Preface 菜鸡HL终于狗来了他的省选停课,这次的时间很长,暂定停到一试结束,不过有机会二试的话还是可以搞到4月了 这段时间的学习就变得量大而且杂了,一般以刷薄弱的知识点和补一些新的奇怪技巧为主. ...
- 2016 ICPC青岛站---k题 Finding Hotels(K-D树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...
- hdu-5992 Finding Hotels(kd-tree)
题目链接: Finding Hotels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/ ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 【BZOJ】【3522】【POI2014】Hotel
暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...
- 【BZOJ】【3831】【POI2014】Little Bird
DP/单调队列优化 水题水题水题水题 单调队列优化的线性dp…… WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还 ...
随机推荐
- 鼓捣phantomjs,做ajax网站的信息采集
版权所有:http://www.cnblogs.com/zeusro/ 引用不给稿费的,切你jj 准备工作: 1phantomjs的安装 2 phantomjs环境变量的配置 需求: 采集手机淘宝某店 ...
- 使用github搭建个人html网站
前言:搭建个人网站早就想做了,最近有空就宅在家学习,突然发现github就可以搭建个人的纯html网站,于是开始了这项工作.转载请注明出处:https://www.cnblogs.com/yuxiao ...
- [AMPPZ2014]Petrol
关键点的最小生成树? 关键点初始化为0,跑多源最短路,然后重构整个图,用Kruskal跑最小生成树 然后跑树链剖分在线回答询问 对树上每个点维护到链顶的最大值,结合线段树可以做到\(\Theta(n ...
- css之背景(background)家族
背景(background)是css中很重要的一部分,也是css的基础知道之一,现在来回顾css2中5个属性与css3中新增的3个属性和2个功能. CSS2_背景(background)前传 家族成员 ...
- 什么是URI,URL以及URN,你真的理解了吗。
先举一个例子,让大家对这三个名词又一个基本的概念: 1⃣️ ftp://ftp.is.co.za/rfc/rfc1808.txt 2⃣️ http://www.cnblogs.com/nods/p/8 ...
- android--Git上克隆项目遇到的坑
直接上图,首先你得有你得GitHub项目地址,如下: 然后打开android studio,选择新建项目时从Git上克隆: 点击clone等待完成,新窗口打开. 打开之后可能.或许.大概.也许会出现下 ...
- okhttp3带cookie请求
Request经常都要携带Cookie,上面说过request创建时可以通过header设置参数,Cookie也是参数之一.就像下面这样: Request request = new Request. ...
- 网络 互联网接入方法、Mbit与MB的转换
ADSL:非对称数字用户环路(绝大多数家庭接入方法,使用电话线).可以提供最高1Mbps的上行速率和最高8Mbps的下行速率.最新的ADSL2+可以提供最高24Mbps的下行速率. 千千兆TB 千兆G ...
- node和iisnode express手工安装
一.安装node.js的x86版本: 这样,node.js会安装在C:\Program Files (x86)\nodejs,这符合iisnode express7版本的期待. 二.安装iisnode ...
- 为什么选用 React 创建混合型移动应用?
[编者按]本文作者为 14islands 联合创始人.创新 Web 开发者 David Lindkvist,主要介绍有关混合型应用搭建的方方面面.文章系国内 ITOM 管理平台 OneAPM 编译呈现 ...