题意

有一个树形结构,每条边的长度相同,任意两个节点可以相互到达。选3个点。两两距离相等。有多少种方案?

1≤n≤5 000

分析

参照小塘空明的题解。

很明显到一个点距离相等的三个点两两之间距离相等。

所以我们枚举该点,对子树进行暴力统计,注意统计的顺序

时间复杂度\(O(n^2)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll; co ll size=5e3+1;
ll n,ans,tot,mx,d[size],tmp[size],s1[size],s2[size];
ll head[size],ver[size*2],next[size*2];
void add(ll x,ll y){
ver[++tot]=y,next[tot]=head[x],head[x]=tot;
}
void dfs(ll x,ll fa){
mx=std::max(mx,d[x]);
tmp[d[x]]++;
for(ll i=head[x];i;i=next[i]){
ll y=ver[i];
if(y==fa) continue;
d[y]=d[x]+1,dfs(y,x);
}
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);
for(ll i=1;i<n;++i){
ll x=read<ll>(),y=read<ll>();
add(x,y),add(y,x);
}
for(ll x=1;x<=n;++x){
memset(s1,0,sizeof s1);
memset(s2,0,sizeof s2);
for(ll i=head[x];i;i=next[i]){
ll y=ver[i];
mx=0,d[y]=1,dfs(y,x);
for(ll j=1;j<=mx;++j){
ans+=s2[j]*tmp[j];
s2[j]+=s1[j]*tmp[j];
s1[j]+=tmp[j];
}
for(ll j=1;j<=mx;++j) tmp[j]=0;
}
}
printf("%lld\n",ans);
return 0;
}

LG3565 [POI2014]HOT-Hotels的更多相关文章

  1. 2016 ICPC青岛站---k题 Finding Hotels(K-D树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...

  2. hdu-5992 Finding Hotels(kd-tree)

    题目链接: Finding Hotels Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 102400/102400 K (Java/ ...

  3. BZOJ 3524: [Poi2014]Couriers [主席树]

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1892  Solved: 683[Submit][St ...

  4. BZOJ 3524: [Poi2014]Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][St ...

  5. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  6. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

  7. 【BZOJ】【3831】【POI2014】Little Bird

    DP/单调队列优化 水题水题水题水题 单调队列优化的线性dp…… WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还 ...

  8. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

  9. BZOJ3522: [Poi2014]Hotel

    3522: [Poi2014]Hotel Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 195  Solved: 85[Submit][Status] ...

随机推荐

  1. Apache的三种工作模式及相关配置

    Apache的三种工作模式 作为老牌服务器,Apache仍在不断地发展,就目前来说,它一共有三种稳定的MPM(Multi-Processing Module,多进程处理模块).它们分别是 prefor ...

  2. Intellij IDEA的一些操作小技巧

    1.Presentation Mode 我们可以使用 Presentation Mode,将IDEA弄到最大,可以让你只关注一个类里面的代码,进行毫无干扰的 coding.可以使用Alt+v快捷键,弹 ...

  3. Spring Boot总结

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

  4. [Java基础] 深入jar包:从jar包中读取资源文件

    转载: http://hxraid.iteye.com/blog/483115?page=3#comments 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等).在单独运行的时候这些简单的 ...

  5. influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB

    influxdb influxdb是最新的一个时间序列数据库,最新一两年才产生,但已经拥有极高的人气.influxdb 是用Go写的,0.9版本的influxdb对于之前会有很大的改变,后端存储有Le ...

  6. JavaScript中的call、apply、bind是怎么回事?

    在JS中,这三者都是用来改变函数的this对象的指向的,他们有什么样的区别呢.在说区别之前还是先总结一下三者的相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this要指向的对 ...

  7. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

  8. 【小米oj】找出单独的数字

    题目链接:https://code.mi.com/problem/list/view?id=2&cid=0&sid=26251#codearea 描述 给出N个数字.其中仅有一个数字出 ...

  9. 3: 组件间的依赖管理 Managing Dependencies Between Components Using the Prism Library 5.0 for WPF(英汉对照版)

    Applications based on the Prism Library are composite applications that potentially consist of many ...

  10. 深入理解java泛型

    一. 什么是泛型? 泛 型(Generic type 或者 generics)是对 简单的理解,就是对类型的参数化,比如我们定义一个类属性或者实例属性时,往往要指定具体的类型,如Integer.Per ...