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的更多相关文章

  1. BZOJ3522: [Poi2014]Hotel

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

  2. BZOJ3522 [Poi2014]Hotel 【树形dp】

    题目链接 BZOJ3522 题解 就是询问每个点来自不同子树离它等距的三个点的个数 数据支持\(O(n^2)\),可以对每个距离分开做 设\(f[i][j]\)表示\(i\)的子树中到\(i\)距离为 ...

  3. BZOJ3522[Poi2014]Hotel——树形DP

    题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...

  4. BZOJ4543/BZOJ3522 [POI2014]Hotel加强版(长链剖分)

    题目好神仙--这个叫长链剖分的玩意儿更神仙-- 考虑dp,设\(f[i][j]\)表示以\(i\)为根的子树中到\(i\)的距离为\(j\)的点的个数,\(g[i][j]\)表示\(i\)的子树中有\ ...

  5. bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4543 题解 这道题的弱化版 bzoj3522 [POI2014]Hotel 的做法有好几种吧. ...

  6. 【BZOJ3522】[Poi2014]Hotel 树形DP

    [BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...

  7. 3522: [Poi2014]Hotel( 树形dp )

    枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) = dist( x , b ) = dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显 ...

  8. 3522: [Poi2014]Hotel

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

  9. 【刷题】BZOJ 4543 [POI2014]Hotel加强版

    Description 同OJ3522 数据范围:n<=100000 Solution dp的设计见[刷题]BZOJ 3522 [Poi2014]Hotel 然后发现dp的第二维与深度有关,于是 ...

随机推荐

  1. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  2. Kinect for Windows SDK开发入门(十九):Kinect Fusion

        Kinect for Windows SDK1.7中引入了Kinect Fusion功能.在1.8的SDK中对该功能进行了改进和强化,Kinect Fusion能够使得我们使用Kinect f ...

  3. docker 常用命令(*)

    查找镜像 https://hub.docker.com/ search  --> centos7 一般docker 会有一个基础镜像,中间件镜像,应用镜像,生成一个镜像 docker build ...

  4. AngularJS模型

    1. AngularJS模型主要就是使用的AngularJS的ng-model指令. ng-model指令可以将输入域的值与 AngularJS 创建的变量绑定. <!DOCTYPE html& ...

  5. java使用tomcat7的maven插件发布到服务器

    服务器容器使用tomcat7的maven插件,使用该插件不需要使用tomcat服务器.以下示例均以paycore项目为例. 一.使用tomcat7-maven-plugin 首先在主pom(payco ...

  6. CocoaPods的那些坑

    CocoaPods的那些坑 文章转自http://blog.csdn.net/zhanniuniu/article/details/52159362#comments 我跟博主的经历超级像!不过自己用 ...

  7. maven+springmvc+dubbo+zookeeper

        为什么要用dubbo?   还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/   一般 nginx+tomcat ...

  8. 因为多余jar包,所报的错

    今天,将项目部署到服务器时,出现了上述的错误.于是就开始找度娘了,找啊找,将项目里面的东西翻来覆去的改,不见效果,还多了些莫名其妙的错,当时就懵逼了.在百度上找到的方法都是说有jar包重复,项目里面的 ...

  9. 关于SharpDevelop 4版本以上没有ILAsm模板项目问题

    学习CIL的时候遇到的问题,记录一下. 查了一下英文网站,SharpDevelop 3 版本有ILAsm模板,但是有人运行3版本的CIL项目有问题. 说明一下SharpDevelop 4版本添加ILA ...

  10. matlab中数组创建方法

    创建数组可以使用 分号 :  逗号, 空格 数组同行用 逗号,或空格分割 不同行元素用 分号: clc; a = [ ]; b1 = a();%第3个元素 b2 = a(:)%第2//4个元素 b3 ...