[POI2014]HOT-Hotels
题目描述
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 过,本蒟蒻表示只会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的更多相关文章
- 2016 ICPC青岛站---k题 Finding Hotels(K-D树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...
- hdu-5992 Finding Hotels(kd-tree)
题目链接: Finding Hotels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/ ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 【BZOJ】【3522】【POI2014】Hotel
暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...
- 【BZOJ】【3831】【POI2014】Little Bird
DP/单调队列优化 水题水题水题水题 单调队列优化的线性dp…… WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还 ...
- Bzoj 3831 [Poi2014]Little Bird
3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...
- BZOJ3522: [Poi2014]Hotel
3522: [Poi2014]Hotel Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 195 Solved: 85[Submit][Status] ...
- Love Hotels and Unicode[转]
原文地址:http://www.reigndesign.com/blog/love-hotels-and-unicode/ 讲得挺通俗的一篇文章 On Sunday 28 October I atte ...
随机推荐
- Python自动化测试、性能测试成长路线图
Python自动化测试成长路线图 性能测试成长路线图
- Asp.net Core 微信公众号开发系列
参考:http://www.cnblogs.com/zskbll/p/4074855.html 一.微信公众平台操作流程 1.先到微信公众平台注册账号 2.登录成功后找到开发-->开发者工具-- ...
- vim操作备忘录
vim操作备忘录 vim 备忘录 vim的书籍虽然看不不少,可是老是容易忘记,主要是自己操作总结过少,这个博客就主要用来记录一些比较常见的术语和操作,以防止自己再次忘记. <leader> ...
- ubuntu python3.6 找不到_sqlite3
sudo apt-get install libsqlite3-dev 然后重新编译python: sudo ./configure --with-ssl sudo make -j8 sudo mak ...
- 在Ubuntu上安装PHPStudy组件
phpStudy for Linux (lnmp+lamp一键安装包) phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS ...
- PHP判断是手机端还是PC端
function check_wap() { if (isset($_SERVER['HTTP_VIA'])) return true; if (isset($_SERVER['HTTP_X_NOKI ...
- 【BZOJ3993】 星际战争
Time Limit: 1000 ms Memory Limit: 128 MB Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一 ...
- 校验Linux程序是否被黑客修改
一个黑客突破你的层层防御后,修改你的程序或者覆盖了你的工具时.确定一个已安装程序的所有文件,有没有被修改过的途径之一就是使用RPM包校验功能 如果图片排版有任何错误,欢迎访问我的简书www.jians ...
- 跨域问题jsonp
不得不说的同源政策: 同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略.所谓同源是指域名,协议,端口相同.当一个浏览器的两个tab页 ...
- Day5模块-shutil模块
参考博客:http://www.cnblogs.com/wupeiqi/articles/4963027.html shutil模块是高级的文件.文件夹.压缩处理的模块.比如文件的copy.压缩等. ...