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. IIS7.0部署MVC/WebApi项目,报404.4错误

    在web.config中添加如下的配置试试: <system.webServer> <modules runAllManagedModulesForAllRequests=" ...

  2. Integrating SharePoint 2013 with ADFS and Shibboleth

    Time again to attempt to implement that exciting technology, Federation Services (Web Single Sign On ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. logback logback.xml常用配置详解 <filter>

    <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY,日志将立即被抛弃不再经过其他过滤器:返回NEUTRAL,有序列表 ...

  5. 【编码】_C#中编码名称(Name)与页面标识(CodePage)的关系_编码gb2312的获取

    在写C#代码时,发现VS提供没有直接提供gb2312的中文编码, 所以,需要找到对应编码名称的codepage来调用想要的编码方式. 下面是微软编程提供的所有编码信息,包括编码名称,编码代码页标识符, ...

  6. IE下get传中文乱码的问题 -- 解决方案 (js)

    W3school 函数 JavaScript 全局对象 定义和用法 encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIs ...

  7. Entity Framework 6连接Postgresql、SQLite、LocalDB的注意事项和配置文件

    Postgresql Postgresql支持Code First的方式自动生成表,不过默认的模式是dbo而不是public,而且还可以自动生成自增主键. <?xml version=" ...

  8. python venv下安装mysql出错 解决方法

    1.首先使用exe文件安装python-mysql.链接: http://pan.baidu.com/s/1kVqILTX 密码: manj. 2.虚拟环境创建后,我们把已经在公共环境使用exe安装好 ...

  9. html5存储方式localstorage和sessionStorage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. 区别:localStorage无时间限制,除非主动删除数据,否则永不过期:sessio ...

  10. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...