题目描述
There are towns in Byteotia, connected with only roads.
Each road directly links two towns.
All the roads have the same length and are two way.
It is known that every town can be reached from every other town via a route consisting of one or more (direct-link) roads.
In other words, the road network forms a tree.
Byteasar, the king of Byteotia, wants three luxury hotels erected to attract tourists from all over the world.
The king desires that the hotels be in different towns and at the same distance one from each other.
Help the king out by writing a program that determines the number of possible locations of the hotel triplet in Byteotia.
输入输出格式
输入格式:
The first line of the standard input contains a single integer (), the number of towns in Byteotia.
The towns are numbered from to .
The Byteotian road network is then described in lines.
Each line contains two integers and () , separated by a single space, that indicate there is a direct road between the towns and .
In tests worth of the total point number an additional condition ![](h…
输出格式:
The first and only line of the standard output should contain a single integer equal to the number of possible placements of the hotels.
输入输出样例
输入样例#1:
7
1 2
5 7
2 5
2 3
5 6
4 5
输出样例#1:
5


哇,英文题。。。

题意

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

有没有瞬间觉得题目变水了。。

思路

大佬这题O(n)过,本蒟蒻表示只会O(n^2)的方法。
首先这三个点一定不形成一条链,一定存在不是这三个点的一个点使这个点到这三个点的距离相等。所以我们直接枚举n个点,Dfs周围每个子树,记录深度,然后乘法原理。

常数巨大的丑陋代码

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <math.h>
using namespace std; # define IL inline
# define RG register
# define UN unsigned
# define ll long long
# define rep(i, a, b) for(RG int i = a; i <= b; i++)
# define per(i, a, b) for(RG int i = b; i >= a; i--)
# define uev(e, u) for(RG int e = ft[u]; e != -1; e = edge[e].nt)
# define mem(a, b) memset(a, b, sizeof(a))
# define max(a, b) ((a) > (b)) ? (a) : (b)
# define min(a, b) ((a) < (b)) ? (a) : (b) IL int Get(){
RG char c = '!'; RG int num = 0, z = 1;
while(c != '-' && (c > '9' || c < '0')) c = getchar();
if(c == '-') z = -1, c = getchar();
while(c >= '0' && c <= '9') num = num * 10 + c - '0', c = getchar();
return num * z;
} const int MAXN = 5001, INF = 2147483647;
struct Edge{
int to, nt;
} edge[MAXN << 1];
int n, ft[MAXN], cnt, out[MAXN], tot[MAXN], deep, t1[MAXN], t2[MAXN];
ll ans; IL void Dfs(RG int u, RG int fa, RG int d){
deep = max(deep, d); tot[d]++;
uev(e, u){
RG int v = edge[e].to;
if(v == fa) continue;
Dfs(v, u, d + 1);
}
} int main(){
mem(ft, -1);
n = Get();
rep(i, 1, n - 1){
RG int u = Get(), v = Get();
edge[cnt] = (Edge){v, ft[u]}; ft[u] = cnt++;
edge[cnt] = (Edge){u, ft[v]}; ft[v] = cnt++;
out[u]++; out[v]++;
}
rep(i, 1, n){
if(out[i] < 3) continue;
mem(t1, 0); mem(t2, 0); deep = 1;
uev(e, i){
RG int v = edge[e].to;
Dfs(v, i, 1);
rep(j, 1, deep){
ans += t1[j] * tot[j];
t1[j] += tot[j] * t2[j];
t2[j] += tot[j];
tot[j] = 0;
}
}
}
printf("%lld\n", ans);
return 0;
}

[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] ...

  10. Love Hotels and Unicode[转]

    原文地址:http://www.reigndesign.com/blog/love-hotels-and-unicode/ 讲得挺通俗的一篇文章 On Sunday 28 October I atte ...

随机推荐

  1. Halcon一日一练:CAD类型的相关操作

    大很多场合,需要在视觉程序中导入CAD文档,比如,在3C行业,需要对手机外壳进行CNC加工,或者点胶操作,此时,需要获取产品的各个点的数据.如果将CAD直接导入,就会大的减少编程工作量,同时也能达到很 ...

  2. 在OS X系统中php访问sftp时需要ssh2扩展的安装

    php -v brew install homebrew/php/php55-ssh2 [实现方式] <?php $connection = ssh2_connect('192.168.0.14 ...

  3. Mysql内置的profiling性能分析工具

    要想优化一条 Query,我们就需要清楚的知道这条 Query 的性能瓶颈到底在哪里,是消耗的 CPU计算太多,还是需要的的 IO 操作太多?要想能够清楚的了解这些信息,在 MySQL 5.0 和 M ...

  4. JS在线生成二维码

    Js代码 百度云公开下载地址:http://pan.baidu.com/s/1nvjTXB7 Html+Php代码 <volist name="huodong_list" i ...

  5. the c programing language 学习过程8

    glean 捡拾落穗; glean insight 深入了解 modeled模型化 peripheral外围的 himogeneous匀称的 intents 意图  excerpt摘录 intende ...

  6. UVA-11214 IDA*

    利用迭代加深搜索,枚举需要的皇后数量,进行搜索. 对于10 * 10 的棋盘,最多需要5个皇后就能攻击整个棋盘,当0~4个皇后都不能搜索成功,那么5就不用搜索,直接打印. AC代码: #include ...

  7. JVM笔记3-java内存区域之运行时常量池

    1.运行时常量池属于线程共享区中的方法区. 2.运行时常量池用于编译期生成的各种自变量,符号引用,这部分内用将在类加载后接入方法区的运行时常量池中存放. 看如下代码所示,如图: public clas ...

  8. Unable to find the ncurses libraries的解决办法

    我们在更新CentOS或者Ubuntu的内核时,执行make menuconfig可能看如这样的错误: *** Unable to find the ncurses libraries or the* ...

  9. V4L2驱动的移植与应用(二)

    二.V4L2的应用 下面简单介绍一下V4L2驱动的应用流程. 1.  视频采集的基本流程 一般的,视频采集都有如下流程: 2.  打开视频设备 在V4L2中,视频设备被看做一个文件.使用open函数打 ...

  10. Alibaba阿里巴巴开源软件列表

    整理和分享我大阿里的开源项目的相关网址: Git Hub上的开源软件网址: 1.https://github.com/alibaba 阿里巴巴开源技术汇总:115个软件 2.https://yq.al ...