BZOJ3522: [Poi2014]Hotel
3522: [Poi2014]Hotel
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 195 Solved: 85
[Submit][Status]
Description
有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达。吉丽要给他的三个妹子各开(一个)房(间)。三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同。
有多少种方案能让吉丽满意?
Input
第一行一个数n。
接下来n-1行,每行两个数x,y,表示x和y之间有一条边相连。
Output
让吉丽满意的方案数。
Sample Input
1 2
5 7
2 5
2 3
5 6
4 5
Sample Output
HINT
【样例解释】
{1,3,5},{2,4,6},{2,4,7},{2,6,7},{4,6,7}
【数据范围】
n≤5000
Source
这题搞了好久时间。。。
直接贴代码吧,我已经记住这道题了。。。
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 5000+5 #define maxm 500+100 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define mod 1000000007 using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; }
int n,tot,head[maxn];
ll f[maxn][],s[maxn];
struct edge{int go,next;}e[*maxn];
inline void insert(int x,int y)
{
e[++tot]=(edge){y,head[x]};head[x]=tot;
e[++tot]=(edge){x,head[y]};head[y]=tot;
}
inline void dfs(int x,int fa,int dep)
{
s[dep]++;
for(int i=head[x];i;i=e[i].next)
if(e[i].go!=fa)dfs(e[i].go,x,dep+);
} int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();
for1(i,n-)insert(read(),read());
ll ans=;
for1(x,n)
{
for(int i=head[x];i;i=e[i].next)
{
dfs(e[i].go,x,);
for1(j,n)
if(s[j])
{
f[j][]+=f[j][]*s[j];
f[j][]+=f[j][]*s[j];
f[j][]+=s[j];
s[j]=;
}
else break;
}
for1(j,n)if(f[j][])ans+=f[j][],f[j][]=f[j][]=f[j][]=;else break;
}
printf("%lld\n",ans); return ; }
比较明显的组合计数用数学来搞比较好,稍微复杂一点的不如用递推式来递推。
UPD:还是写一下题解吧。。。
我们枚举每个点做根,那么合法的方案就是在不同的子树中取出3个dep相同的节点的方案数,每dfs一棵子树,我们就用它的信息来更新ans,注意信息用了就要清空。
BZOJ3522: [Poi2014]Hotel的更多相关文章
- BZOJ3522 [Poi2014]Hotel 【树形dp】
题目链接 BZOJ3522 题解 就是询问每个点来自不同子树离它等距的三个点的个数 数据支持\(O(n^2)\),可以对每个距离分开做 设\(f[i][j]\)表示\(i\)的子树中到\(i\)距离为 ...
- BZOJ3522——[Poi2014]Hotel
1.题意:http://www.lydsy.com/JudgeOnline/problem.php?id=3522 2.分析:这道题有两种情况,第一个是三个点在同一个链上,这显然不可能了,因为树上的路 ...
- BZOJ3522[Poi2014]Hotel——树形DP
题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...
- BZOJ4543/BZOJ3522 [POI2014]Hotel加强版(长链剖分)
题目好神仙--这个叫长链剖分的玩意儿更神仙-- 考虑dp,设\(f[i][j]\)表示以\(i\)为根的子树中到\(i\)的距离为\(j\)的点的个数,\(g[i][j]\)表示\(i\)的子树中有\ ...
- bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4543 题解 这道题的弱化版 bzoj3522 [POI2014]Hotel 的做法有好几种吧. ...
- 【BZOJ3522】[Poi2014]Hotel 树形DP
[BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...
- 3522: [Poi2014]Hotel( 树形dp )
枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) = dist( x , b ) = dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显 ...
- 3522: [Poi2014]Hotel
3522: [Poi2014]Hotel Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 253 Solved: 117[Submit][Status ...
- 【刷题】BZOJ 4543 [POI2014]Hotel加强版
Description 同OJ3522 数据范围:n<=100000 Solution dp的设计见[刷题]BZOJ 3522 [Poi2014]Hotel 然后发现dp的第二维与深度有关,于是 ...
随机推荐
- #ifndef#define#endif的用法
在网上看到了感觉作者总结得很好,作者辛苦了! #ifndef#define#endif的用法 文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都 ...
- struts2的单文件下载
一. 导入两个jar包 commons-fileupload-1.3.1.jar commons-io-2.4.jar 二.编写请求上传jsp <h1>文件列表--单文件</h1&g ...
- nginx图片服务器配置
worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/erro ...
- cyark - 数字方舟(看侣行第三季时发现的)
数字方舟 Ben Kacyra
- 模板:优先队列(priority_queue)
#include <iostream> #include <cstdio> #include <queue> #include <vector> usi ...
- Headfirst设计模式的C++实现——装饰者模式(Decorater)
Beverage.h #ifndef BEVERAGE_H_INCLUDED #define BEVERAGE_H_INCLUDED #include <string> class Bev ...
- C++11中新特性之:initializer_list详解
C++11提供的新类型,定义在<initializer_list>头文件中. template< class T > class initializer_list; 先说它的用 ...
- mysql复制表数据或表结构到新表中
MySQL复制表数据到新表的几个步骤. 1.MySQL复制表结构及数据到新表 CREATE TABLE new_table SELECT * FROM old_table; 2.只复制表结构到新表 C ...
- 易买网(注册Ajax讲解)
关于注册(用到Ajax) 运用onblur进行时时刷新 创建所需用的Servlet 好了 Ajax其实不是很难 如果还是不懂可以私信我呦-^^-!
- Uploadify 上传失败
更改Apache的php.ini的配置 In my php.ini (created custom ini file in public_html) would this solve this pro ...