HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)
If you solved the last problem, you will see that the devil can't figure out who is z*p because there are too many people. So anyway, she decided to let it go.
The devil think she is the cutest loli in the world, but there is some people in the kingdom don't think so. And they think WJMZBMR is the most cutest loli.
It seems a war is approaching, but in this kingdom, due to the old tradition, every conflict is solved by Algorithm contest!
One day, WJMZBMR is hanging out with her friend s***kplus on the street. And she noticed that there is a group of people playing algorithm contest to decide who is the cutest loli in the kingdom. One problem attracts her interest:
Given a tree with n vertices, we randomly choose k vertices of it. Then we can induced a subtree which is the smallest connected subtree of the original tree containing those k vertices.
Each vertex has a label(an integer), for a subtree we induced, we look at its diameter a-b,(if there are many diameters, pick the one with the smallest a, and then the smallest b). And output how many distinct label are on the diameter.
What is the expected value we output?
Of course, WJMZBMR is merely a cute loli and don't know much about the algorithm contest, but since you are a member of Princess's Knight, you should solve it for your princess, can you do it?
For each test case, the first line contains two integers n,k.
The next n-1 lines, each contains two integers a and b, denote there is an edge between a and b.
The next line contains n integers separated by a single space, denote each vertex's label in the order from 1 to n.
n,k <= 300. label <= 10^9.
T <= 20.
This problem is special judged. The relative error less than 1e-6 will be accepted.
题目大意:给一棵树,每个点有一个权值,边长为1。随机选择k个点,选上使得这k个点连通的最少点,连上边,设其字典序最小的直径为path(a, b),令path(a, b)上不同权值的点的个数为t。求t的期望。
思路:先放上CLJ的官方题解:
不妨枚举a,b作为直径,然后计算该情况的概率。注意到如果不考虑字典序这其实等价于其它选的点满足Dc→a ≤ Da→b 并且 Dc→b ≤ Da→b。
考虑字典序也类似。
首先这里是一条求树的直径的题目:POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
这里面的第一个解法的证明可以得出一个结论①:从一个点开始搜索,离这个点最远的点,一定是这棵树的其中一个直径的一个端点。
设mat[a][b]为a→b的简单路径的距离,下面设a < b。
结论②:对于a、b两点,把所有max(mat[i][a], mat[i][b]) ≤ mat[a][b]的点选上,其诱导子图中,path(a, b)是其中一条直径。
证明:利用由mat[i][a] ≤ mat[a][b]得,b是离a最远的点之一。由结论①得,b是直径的端点。同理可得a是离b最远的点之一,那么可得path(a, b)是一条直径。
结论③:在②的前提下,选择点 i 当且仅当没有(mat[i][b] == mat[a][b] && i < a)和(mat[i][a] == mat[a][b] && i < b),此时path(a, b)是字典序最小的直径。
必要性:若有mat[i][b] == mat[a][b] && i < a,那么有直径path(i, b),字典序小于path(a, b),否决。若有mat[i][a] == mat[a][b] && i < b,那么有直径path(a, i),字典序小于path(a, b)。
充分性:若存在直径path(i, a),那么i > b > a,那么path(a, i)字典序小于path(i, a),path(a, i)字典序小于path(a, b)。
若存在直径path(b, i),由于b > a且i > a,则path(b, i)与path(i, b)字典序小于path(a, b)。
假设存在直径path(i, j)。那么有mat[i][j] = mat[a][b]。这种情况出现当且仅当path(i, j)和path(a, b)的交汇点是path(a, b)的中点,此时有path(i, a) = path(j, a) = path(a, b),由上面的可知,(i, j)的字典序小于path(a, b)。
结论④:结论③所诱导出来的子图是符合条件的极大诱导子图。
证明:随意加入一个点,path(a, b)都不再是字典序最小的直径。
先预处理出每对点之间的距离mat[a][b],再预处理出每对点之间不同权值的点的个数dif[a][b]。
在预处理出阶乘的对数,算组合数c(n, k)的时候使用阶乘来O(1)计算,算好后再exp一下,提高精度。至于不这么做能不能过我就不知道了,这是我能想到精度最高的方法了。
接下来,就是枚举a、b。其中a < b。对于每个(a, b),若子图点数cnt大于k个点,那么累加ans += dif[a][b] * c(cnt - 2, k - 2) / c(n, k)。
PS:k=1时这里输出0。几个小时的人生。
代码(1453MS):
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f; int mat[MAXN][MAXN], dif[MAXN][MAXN];
int label[MAXN], hash[MAXN], tmp[MAXN];
int n, k, T; int head[MAXN], ecnt;
int to[MAXN << ], next[MAXN << ]; void init() {
memset(head + , -, n * sizeof(int));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} int que[MAXN], dis[MAXN]; void bfs(int st) {
memset(dis + , 0x3f, n * sizeof(int));
int l = , r = ;
dis[que[r++] = st] = ;
while(l != r) {
int u = que[l++];
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(dis[u] + < dis[v]) {
dis[v] = dis[u] + ;
que[r++] = v;
}
}
}
for(int i = ; i <= n; ++i) mat[st][i] = dis[i];
} int cnt[MAXN]; void dfs(int u, int f, int& c, int st) {
if(++cnt[hash[u]] == ) c++;
dif[st][u] = c;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs(v, u, c, st);
}
if(--cnt[hash[u]] == ) c--;
}
//排列组合
double per[MAXN];
void initPer(int n = ) {
per[] = log();
for(int i = ; i <= n; ++i)
per[i] = per[i - ] + log(i);
}
double c(int n, int k) {
return per[n] - per[n - k] - per[k];
} double calc(int a, int b) {
int cnt = ;
for(int i = ; i <= n; ++i) {
if(i == a || i == b) continue;
if(mat[i][a] > mat[a][b] || mat[i][b] > mat[a][b]) continue;
if(mat[i][b] == mat[a][b] && i < a) continue;
if(mat[i][a] == mat[a][b] && i < b) continue;
cnt++;
}
if(cnt >= k) return dif[a][b] * exp(c(cnt - , k - ) - c(n, k));
else return ;
} double solve() {
if(n < k || k == ) return ;
if(k == ) return ;
double res = ;
for(int i = ; i <= n; ++i) {
for(int j = i + ; j <= n; ++j) {
res += calc(i, j);
}
}
return res;
} int main() {
initPer();
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &k);
init();
for(int i = , a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
add_edge(a, b);
}
for(int i = ; i <= n; ++i) bfs(i); for(int i = ; i <= n; ++i) scanf("%d", &hash[i]);
int cnt = ;
for(int i = ; i <= n; ++i) tmp[cnt++] = hash[i];
sort(tmp, tmp + cnt);
cnt = unique(tmp, tmp + cnt) - tmp;
for(int i = ; i <= n; ++i) hash[i] = lower_bound(tmp, tmp + cnt, hash[i]) - tmp;
for(int i = , c = ; i <= n; ++i) dfs(i, , c, i); printf("%.10f\n", solve());
}
}
HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)的更多相关文章
- hdu 2818 Building Block(加权并查集)2009 Multi-University Training Contest 1
题意: 一共有30000个箱子,刚开始时都是分开放置的.接下来会有两种操作: 1. M x y,表示把x箱子所在的一摞放到y箱子那一摞上. 2. C y,表示询问y下方有多少个箱子. 输入: 首行输入 ...
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...
- hdu 3461 Code Lock(并查集)2010 ACM-ICPC Multi-University Training Contest(3)
想不到这还可以用并查集解,不过后来证明确实可以…… 题意也有些难理解—— 给你一个锁,这个所由n个字母组成,然后这个锁有m个区间,每次可以对一个区间进行操作,并且区间中的所有字母要同时操作.每次操作可 ...
- hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10
搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...
- HDU 6356.Glad You Came-线段树(区间更新+剪枝) (2018 Multi-University Training Contest 5 1007)
6356.Glad You Came 题意就是给你一个随机生成函数,然后从随机函数里确定查询的左右区间以及要更新的val值.然后最后求一下异或和就可以了. 线段树,区间最大值和最小值维护一下,因为数据 ...
- HDU 4610 Cards (合数分解,枚举)
Cards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 4326Game(比较难理解的概率dp)
Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- HDU 4336 Card Collector(动态规划-概率DP)
Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...
- HDU校赛 | 2019 Multi-University Training Contest 6
2019 Multi-University Training Contest 6 http://acm.hdu.edu.cn/contests/contest_show.php?cid=853 100 ...
随机推荐
- [dpdk] 读开发指南(1)
该文档是随着对于文档的阅读进度,不断增加的阅读笔记.主要内容以大纲为主,以及记录帮助记忆的内容. 在之后的实际应用中,也不随着不断的深入理解,逐渐丰富各大纲下面的内容. 1. 前期准备:设置两个环境变 ...
- html代码转义到js时,往往会遇到问题,这代码实现html和js互转
这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 介绍UDF,以及完成大小写的转换
一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多出 3.udf的实现步骤 继承UDF类 实现evaluate的 ...
- 【Android测试】【第七节】Monkey——源码浅谈
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4713466.html 前言 根据上一篇我们学会了Monke ...
- eclipse根据.wsdl文件自动生成webservice的调用客户端
1.工具:eclipse3.3或者是带有webservice插件的eclipse 2. 首先用浏览器访问webservice的站点,接着保存打开的页面,后缀为.wsdl. 3.把保存好的文件拷入ecl ...
- Magento white screen or how XML can break your site?
Magento white screen or how XML can break your site? by SANDO on 02. OCT, 2012 in MAGENTO, SMALL TIP ...
- MSP430之Hello World!
//#include "io430.h" #include "MSP430G2553.h" int main( void ) { volatile unsign ...
- Android 关于ExpandableListView控件setOnChildClickListener无效问题
其实很简单,在适配器里面重写isChildSelectable的时候返回值切记为true,这样才能使得二级监听有响应. 其次注意继承的是BaseExpandableListAdapter
- 使用 tox flake8 pytest 规范 python 项目
使用 tox flake8 pytest 规范 python 项目 python 中有些很好的工作来规范整个项目的开发,而其中使用较多的就是使用 tox . flake8 . pytest . tox ...
- 关于ios导航控制器的知识总结
关于ios导航控制器的知识总结 添加了导航控制器后: 1.一个导航控制器会有一个顶部导航栏navigationbar和一个底部工具栏toolbar,它们是导航控制器navC的属性.且导航栏默认是不隐藏 ...