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的第二维与深度有关,于是 ...
随机推荐
- ngx_http_uwsgi_module模块.md
ngx_http_uwsgi_module ngx_http_uwsgi_module模块允许将请求传递到uwsgi服务器. 示例配置: location / { include uwsgi_para ...
- [原]CentOS7部署PostGis
转载请注明原作者(think8848)和出处(http://think8848.cnblogs.com) 本文参考了<An almost idiot's guide to install Pos ...
- ASP.NET提取多层嵌套json数据的方法
本文实例讲述了ASP.NET利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,具体例子如下. 假设需要提取的json字符串如下: {"name":&quo ...
- 队列&生产者消费者
Queue是python标准库中的线程安全的队列(FIFO)实现,提供了一个适用多线程的先进先出的数据结构,即队列,用来在生产者和消费者线程之间信息传递. 如果在多线程中,给存放数据,也就是修改同一份 ...
- Quartz定时任务
spring多个定时任务quartz配置 例子1: biz-quartz-context.xml配置 <?xml version="1.0" encoding="U ...
- 研究Extension和Category的一个例子
Category: 1. 无法添加实例变量 2.将类的实现分散到多个不同文件或多个不同框架中. Extension: 1. 可以添加实例变量 注: 如果Category的头文件中也使用Extensio ...
- Power BI REST API
Overview of Power BI REST API https://msdn.microsoft.com/en-us/library/dn877544.aspx 验证方式 OAuth 2.0 ...
- SignalR实现网页实时聊天功能
SignalR是利用html5 sokit方式实现网页的实时性,在客户端不支持html5的情况下通过轮询实现 实现原理是客户端发送的消息先去服务器,然后服务器根据需要将消息广播到需要接收信息的客户群. ...
- Python 3.5源码编译安装
系统环境:CentOS 6.8-Minimal 安装Python依赖包: [root@Python src]# yum install zlib-devel bzip2-devel openssl-d ...
- [bigdata] hadoop 参数配置解析
ResourceManager相关配置参数 yarn-site.xml 中配置 yarn.resourcemanager.address ResourceManager 对客户端暴露的地址.客户端通过 ...