[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 ...
随机推荐
- testng 异常 截图
testNG里有一个异常监听类,失败时会执行类里的相关方法 DriverBase 截图类 TestngListenerScreen 异常监听类 Test1 测试类1.DriverBase类 packa ...
- Openwrt上使用dnsmasq和ipset实现域名分流
目标 部署一台自动代理路由器,实现根据域名来自动设定直连或者代理,而我要做的只是设置PC的默认网关为主路由器(192.168.0.1)还是自动代理路由器(192.168.0.254). 创建Openw ...
- 9、flask之SQLAlchemy
本篇导航: 介绍 使用 SQLAlchemy-Utils 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之 ...
- selenium自动化测试——常见的八种元素定位方法
selenium常用的八种元素定位方法 1.通过 id 定位:find_element_by_id() 2.通过 name 定位:find_element_by_name() 3.通过 tag 定位: ...
- IE8兼容问题总结---trim()方法
1.IE8不支持,jquery的trim()去空格的方法 错误表现 : 会报错,对象不支持此属性或方法; 解决办法 : 使用正则匹配空格 例如 : /^\s+|\s+$/greplace(/^\s+| ...
- struts 中的创建Action的三种方法
1.对于直接创建类,不实现接口和继承任何的类 例如创建一个helloAction package cn.lonecloud.control; import com.opensymphony.xwork ...
- 老男孩Python全栈开发(92天全)视频教程 自学笔记06
day6课程内容: tuple(元祖) 创建元祖: tup0=() #没有元素的一个元祖 tup1=(20,)#只有一个元素的元祖 元祖可读,不可修改 Dictionary(字典)#Python里唯一 ...
- Ajax检测用户名是否已经注册
程序功能 当用户名输入完成(即用户名输入框失去焦点),利用Ajax检测用户名是否已经注册! 实现过程 利用Ajax向CheckUserServlet发送请求,判断该用户名是否可用.这里只是为了演示Aj ...
- POJ - 2912 Rochambeau 种类并查集
题意:有三组小朋友在玩石头剪刀布,同一组的小朋友出的手势是一样的.这些小朋友中有一个是裁判,他可以随便出手势.现在给定一些小朋友的关系,问能否判断出裁判,如果能最早什么时候能够找到裁判. 思路:枚举每 ...
- Subsequence Count (线段树)
Time Limit: 1000 ms Memory Limit: 256 MB Description 给定一个01串 $S_{1 \cdots n}$ 和 $Q$ 个操作. 操作有两种类型: ...