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 ...
随机推荐
- SQL Server 触发器【转】
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
- pro8
1.本次课学到的知识点 函数程序设计 结构化程序设计思想 程序解析 局部变量和全局变量 2.实验过程中遇到的问题及解决方法 实验过程中会遇到自定义函数的逻辑错误 与缺少定义变量 从主函数开始理清函数关 ...
- nrf51822裸机教程-PPI
Programmable Peripheral Interconnect即可编程外设互联 系统,该模块是51822 提供的一个特性. 目的是为了让51822 的外围模块可以不通过处理器而自动相互作用. ...
- [LeetCode]题解(python):041-First Missing Positive
题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...
- raspberryPi 拍照
调用python的库,学习raspberryPi的摄像头操作方法. 参考链接: https://www.raspberrypi.org/learning/getting-started-with-pi ...
- 详细解读Jquery的$.get(),$.post(),$.ajax(),$.getJSON()用法
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表,callback为请求成功后的回调函数,该函数接受两个参数,第一个为服务器返回的数据,第 ...
- NSNumber,NSValue和NSData
我们在编码中,很多时候需要将C里面原生的数据封装成对象,这样可以用NSDictionary或者NSArray来存取访问.尤其是一些做适配的情况下,这种封装是不可避免的.Objective-C提供了不少 ...
- 源码搭建SVN+Apache+Setpass
1.安装配置apache2.2.18 http://download.csdn.net/download/YH555/3299526tar xf httpd-2.2.18.tar.bz2cd http ...
- error LNK2005 int __cdecl 解决方案【转】
error LNK2005: "int __cdecl isPtInPolygon(class std::vector<struct double2,class std::alloca ...
- Linux下设置网卡静态ip
Linux下设置网卡静态ip 如果是服务器版,没有图形界面只用用命令行修改配置文件 如果是客户端版本,可以用图形界面 配置的前提是要在root用户下才能重启网卡服务 图形界面: system-conf ...