[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 Study Notes]字符串处理技巧(持续更新)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]计算cpu使用率
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- WPF项目学习.三
工具代码记录 版权声明:本文为博主初学经验,未经博主允许不得转载. 一.前言 记录在学习与制作WPF过程中遇到的解决方案. 分页控件的制作,邮件发送,日志代码,excel导入导出等代码的实现过程: 二 ...
- JSP基础点滴
注释:<%-- 注释 --%> JSP中一共有3种Scriptlet代码.支持与HTML的代码混编. 第一种:<%%> 定义局部变量,编写语句. 第二种:<%!%> ...
- redis —主从&&集群(CLUSTER)
REDIS主从配置 为了节省资源,本实验在一台机器进行.即,在一台机器上启动两个端口,模拟两台机器. 机器准备: [root@adailinux ~]# cp /etc/redis.conf /etc ...
- Docker安装Jenkins
1.下载镜像 docker pull jenkins 2.生成一个容器 docker run -d --name myjenkins -p 8081:8080 -p 50000:50000 --vo ...
- selenium自动化测试——常见的八种元素定位方法
selenium常用的八种元素定位方法 1.通过 id 定位:find_element_by_id() 2.通过 name 定位:find_element_by_name() 3.通过 tag 定位: ...
- CSS布局(四) float详解
一.float设计初衷 因为float被设计出来的初衷是用于--文字环绕效果.即,一个图片一段文字,图片float:left之后,文字会环绕图片. <div style="width: ...
- Qt 信号如何自动连接槽函数?
on_objectName_signal [static] void QMetaObject::connectSlotsByName(QObject *object) void on_<obje ...
- 《android开发进阶从小工到专家》读书笔记--HTTP网络请求
No1: 客户端与服务器的交互流程: 1)客户端执行网络请求,从URL中解析出服务器的主机名 2)将服务器的主机名转换成服务器的IP地址 3)将端口号从URL中解析出来 4)建立一条从客户端与Web服 ...