【bzoj3522】 [Poi2014]Hotel
来源:互联网 发布:mx anywhere2 软件 编辑:程序博客网 时间:2024/06/09 05:37
Description
有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达。吉丽要给他的三个妹子各开(一个)房(间)。三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同。
有多少种方案能让吉丽满意?
Input
第一行一个数n。
接下来n-1行,每行两个数x,y,表示x和y之间有一条边相连。
Output
让吉丽满意的方案数。
Sample Input
7
1 2
5 7
2 5
2 3
5 6
4 5
1 2
5 7
2 5
2 3
5 6
4 5
Sample Output
5
HINT
【样例解释】
{1,3,5},{2,4,6},{2,4,7},{2,6,7},{4,6,7}
【数据范围】
n≤5000
先枚举一个点,然后dfs计数
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int N=5005;int fi[N],ne[N<<1],j[N<<1];int n,mx;long long t1[N],t2[N],s[N],ans;void dfs(int ro,int fa,int dep){ mx=max(mx,dep);s[dep]++; for (int u=fi[ro];u;u=ne[u]) if (j[u]!=fa)dfs(j[u],ro,dep+1);}int main (){ scanf ("%d",&n); for (int i=1;i<n;++i){ scanf ("%d%d",&j[i+n],&j[i]); ne[i]=fi[j[i+n]];fi[j[i+n]]=i; ne[i+n]=fi[j[i]];fi[j[i]]=i+n; } for (int i=1;i<=n;++i){ memset(t1,0,sizeof(t1)); memset(t2,0,sizeof(t2)); for (int u=fi[i];u;u=ne[u]){ dfs(j[u],i,1); for (int k=1;k<=mx;++k){ ans+=t2[k]*s[k]; t2[k]+=t1[k]*s[k]; t1[k]+=s[k]; s[k]=0; } } } printf ("%lld",ans); return 0;}
阅读全文
0 0
- [BZOJ3522] [Poi2014]Hotel
- 【BZOJ3522】[Poi2014]Hotel【DFS】
- BZOJ3522: [Poi2014]Hotel Dfs
- 【bzoj3522】[Poi2014]Hotel
- 【bzoj3522】 [Poi2014]Hotel
- [bzoj3522][bzoj4543][POI2014]HOTEL
- BZOJ3522 [Poi2014]Hotel
- [杂题] BZOJ3522: [Poi2014]Hotel
- 【bzoj3522】[Poi2014]Hotel
- Bzoj3522:[Poi2014]Hotel:树形DP:
- 【bzoj3522】【poi2014】【hotel】【树形dp】
- BZOJ3522——[Poi2014]Hotel
- 【bzoj3522】[Poi2014]Hotel 暴力+计数
- [bzoj3522][Poi2014]Hotel(暴力)
- 【bzoj3522】[Poi2014]Hotel 树形dp
- BZOJ4543/BZOJ3522 [POI2014]Hotel加强版
- BZOJ3522&4543: [POI2014]Hotel加强版
- bzoj3522 [Poi2014]Hotel dfs(DP)
- cf 832D Misha, Grisha and Underground 【lca+倍增】
- POJ3648 A Simple Problem with Integers(树状数组实现)
- Android 系统发送“系统当前时间”的广播
- Selenium Grid 的使用
- 小白笔记-----------------------Metasploit (from tools)整理
- 【bzoj3522】 [Poi2014]Hotel
- Netty echo server 简例
- 奇偶位交换
- Mr.Smile填坑记——debug、release包MD5签名的获取方式
- redis写入文件
- codeforces—— 489B —— BerSU Ball
- eslint 的三大通用规则
- android studio 将已有的项目 以module Library的 形式引入到 现在的项目中可能碰到的一些问题
- 写高质量OC代码52建议总结:47.熟悉系统架构