BZOJ3522——[Poi2014]Hotel
1、题意:http://www.lydsy.com/JudgeOnline/problem.php?id=3522
2、分析:这道题有两种情况,第一个是三个点在同一个链上,这显然不可能了,因为树上的路径是唯一的,第二个情况是三个点距离某个中心的距离相等
那么我们只需枚举中间点,然后在不同子树中dfs一下即可求出答案
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define M 10010
#define LL long long
inline int read(){
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct Edge{
int u, v, next;
} G[M];
int head[M], tot;
int q[M], fa[M];
int tmp[M / 2], g[M / 2], f[M / 2];
inline void add(int u, int v){
G[++ tot] = (Edge){u, v, head[u]};
head[u] = tot;
}
inline void dfs(int x, int fa, int deep){
tmp[deep] ++;
for(int i = head[x]; i != -1; i = G[i].next) if(G[i].v != fa){
dfs(G[i].v, x, deep + 1);
}
}
int main(){
int n = read();
memset(head, -1, sizeof(head));
for(int i = 1; i < n; i ++){
int u = read(), v = read();
add(u, v); add(v, u);
}
LL ans = 0;
for(int i = 1; i <= n; i ++){
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
for(int j = head[i]; j != -1; j = G[j].next){
memset(tmp, 0, sizeof(tmp));
dfs(G[j].v, i, 1);
for(int k = 1; k <= n; k ++){
ans += (LL)(g[k] * tmp[k]);
g[k] += f[k] * tmp[k];
f[k] += tmp[k];
}
}
}
printf("%lld\n", ans);
return 0;
}
BZOJ3522——[Poi2014]Hotel的更多相关文章
- BZOJ3522: [Poi2014]Hotel
3522: [Poi2014]Hotel Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 195 Solved: 85[Submit][Status] ...
- BZOJ3522 [Poi2014]Hotel 【树形dp】
题目链接 BZOJ3522 题解 就是询问每个点来自不同子树离它等距的三个点的个数 数据支持\(O(n^2)\),可以对每个距离分开做 设\(f[i][j]\)表示\(i\)的子树中到\(i\)距离为 ...
- BZOJ3522[Poi2014]Hotel——树形DP
题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...
- BZOJ4543/BZOJ3522 [POI2014]Hotel加强版(长链剖分)
题目好神仙--这个叫长链剖分的玩意儿更神仙-- 考虑dp,设\(f[i][j]\)表示以\(i\)为根的子树中到\(i\)的距离为\(j\)的点的个数,\(g[i][j]\)表示\(i\)的子树中有\ ...
- bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4543 题解 这道题的弱化版 bzoj3522 [POI2014]Hotel 的做法有好几种吧. ...
- 【BZOJ3522】[Poi2014]Hotel 树形DP
[BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...
- 3522: [Poi2014]Hotel( 树形dp )
枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) = dist( x , b ) = dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显 ...
- 3522: [Poi2014]Hotel
3522: [Poi2014]Hotel Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 253 Solved: 117[Submit][Status ...
- 【刷题】BZOJ 4543 [POI2014]Hotel加强版
Description 同OJ3522 数据范围:n<=100000 Solution dp的设计见[刷题]BZOJ 3522 [Poi2014]Hotel 然后发现dp的第二维与深度有关,于是 ...
随机推荐
- webgl画个点
function main(){ var canvas = document.getElementById("webgl"); var gl = getWebGLContext(c ...
- Anna-senpai帖子翻译与Mirai源代码使用
Anna-senpai这个人太好玩了,整件事就像没有黄段子的无聊世界那样. 无聊翻译了一下,顺便实验了效果. --------------------------------------------- ...
- js自定义验证码
分享一个js自定义的验证码 window.onload = function () { var code; $('.changePassword').click(function () ...
- C++远征之封装篇(下)
对象数组 类 x1[]; 栈中实例化,不用delete. 类 *X=new X[];//在堆中实例化,结尾需要用delete删除 delete []X; //这是数组的删除形式 X=NULL;
- jqGrid jqGrid分页参数+条件查询
HTML <div class="row"> <div class="col-sm-20"> <form id="for ...
- javascript 中 Promise的使用
有点经验的js前端都知道 ajax异步函数里面的结果不会立即返回,如果你想在一个异步函数得到某个结果后去执行一个语句怎么做? if ( 异步函数 ) { 语句 } 可能很多人都踩过这样坑,这个时候 ...
- 【bzoj1941】 Sdoi2010—Hide and Seek
http://www.lydsy.com/JudgeOnline/problem.php?id=1941 (题目链接) 题意 给出n个二维平面上的点,求一点使到最远点的距离-最近点的距离最小. Sol ...
- django用户认证
利用django自带认证功能实现用户登录认证. views.py # Create your views here. from django.shortcuts import render_to_re ...
- docker 配置操作指导
1.下载程序包 安装包 https://github.com/boot2docker/windows-installer/releases(这个地址国内下载很慢) 或这个:http://www.sof ...
- window frida安装
当需要安装第三方python包时,可能会用到easy_install命令.easy_install是由PEAK(Python Enterprise Application Kit)开发的setupto ...