3522: [Poi2014]Hotel
来源:互联网 发布:linux nginx自动启动 编辑:程序博客网 时间:2024/05/14 20:59
3522: [Poi2014]Hotel
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 572 Solved: 334
[Submit][Status][Discuss]
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
Source
By Dzy
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<queue>#include<set>#include<map>#include<stack>#include<bitset>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int maxn = 5005;typedef long long LL;int n,Cnt,I,vis[maxn],L[maxn],cnt[maxn],f[maxn][maxn],B[maxn],C[maxn],MaxL[maxn];LL Ans;vector <int> v[maxn];bool cmp(const int &x,const int &y) {return MaxL[x] > MaxL[y];}void Dfs(int x,int y){++f[y][L[x]]; ++cnt[L[x]]; MaxL[y] = max(MaxL[y],L[x]);for (int i = 0; i < v[x].size(); i++){int to = v[x][i]; if (vis[to] == Cnt) continue;vis[to] = Cnt; L[to] = L[x] + 1; Dfs(to,y);}}void Work(int i){int tail = v[i].size() - 1;for (int j = 1; j <= n; j++){while (tail >= 0 && MaxL[v[i][tail]] < j) --tail; if (tail < 0) break; B[tail+1] = C[tail+1] = 0;for (int t = tail; t >= 0; t--){int son = v[i][t]; B[t] = f[son][j]; C[t] = f[son][j]*f[son][j];Ans += 1LL*(cnt[j]*B[t]*B[t+1] - C[t]*B[t+1] - B[t]*C[t+1]);B[t] += B[t+1]; C[t] += C[t+1];}}for (int j = 0; j < v[i].size(); j++){int son = v[i][j];for (int t = 1; t <= MaxL[son]; t++)f[son][t] = 0;}}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifcin >> n;for (int i = 1; i < n; i++){int x,y; scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);}for (int i = 1; i <= n; i++){memset(cnt,0,sizeof(cnt)); vis[i] = ++Cnt;for (int j = 0; j < v[i].size(); j++){int to = v[i][j]; L[to] = 1; vis[to] = Cnt; MaxL[to] = 0; Dfs(to,to);}sort(v[i].begin(),v[i].end(),cmp); Work(i);}cout << Ans / 3LL;return 0;}
0 0
- 3522: [Poi2014]Hotel
- BZOJ 3522: [Poi2014]Hotel
- BZOJ 3522 Poi2014 Hotel DFS
- BZOJ 3522 & 4543: [POI2014]Hotel
- 3522: [Poi2014]Hotel 树形dp
- bzoj 3522: [Poi2014]Hotel dfs
- [树形DP 启发式合并 神题] BZOJ 4543 [POI2014]Hotel加强版 & BZOJ 3522 [Poi2014]Hotel
- [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
- BZOJ4543/3522: [POI2014]Hotel加强版(洛谷P3565)
- Bzoj3522:[Poi2014]Hotel:树形DP:
- android pulltorefreshlistview 左右滑动如何实现?
- android TV 焦点 问题
- 堆、栈、自由存储区、全局/静态存储区和常量存储区
- align text-align vertical-align
- kafka-2.8-0.8.0安装
- 3522: [Poi2014]Hotel
- 1123. Is It a Complete AVL Tree (30)解题报告
- java 刷题自己写的工具类
- svm1
- Unable to convert MySQL date/time value to System.DateTime 错误
- 注意力机制(Attention Mechanism)在自然语言处理中的应用
- android studio adb无法启动的一个异常
- Structs2框架学习 Day2
- Java8 Stream的几种生成方式