HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4900
Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

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?
 
Input
The first line contains an integer T, denoting the number of the test cases.
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.
 
Output
For each case, output the result.
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的官方题解:

    不妨枚举ab作为直径,然后计算该情况的概率。注意到如果不考虑字典序这其实等价于其它选的点满足Dc≤ Dab  并且 Dc≤ Dab

    考虑字典序也类似。

首先这里是一条求树的直径的题目: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):

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 using namespace std;
  7 
  8 const int MAXN = 310;
  9 const int INF = 0x3f3f3f3f;
 10 
 11 int mat[MAXN][MAXN], dif[MAXN][MAXN];
 12 int label[MAXN], hash[MAXN], tmp[MAXN];
 13 int n, k, T;
 14 
 15 int head[MAXN], ecnt;
 16 int to[MAXN << 1], next[MAXN << 1];
 17 
 18 void init() {
 19     memset(head + 1, -1, n * sizeof(int));
 20     ecnt = 0;
 21 }
 22 
 23 void add_edge(int u, int v) {
 24     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
 25     to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
 26 }
 27 
 28 int que[MAXN], dis[MAXN];
 29 
 30 void bfs(int st) {
 31     memset(dis + 1, 0x3f, n * sizeof(int));
 32     int l = 0, r = 0;
 33     dis[que[r++] = st] = 0;
 34     while(l != r) {
 35         int u = que[l++];
 36         for(int p = head[u]; ~p; p = next[p]) {
 37             int &v = to[p];
 38             if(dis[u] + 1 < dis[v]) {
 39                 dis[v] = dis[u] + 1;
 40                 que[r++] = v;
 41             }
 42         }
 43     }
 44     for(int i = 1; i <= n; ++i) mat[st][i] = dis[i];
 45 }
 46 
 47 int cnt[MAXN];
 48 
 49 void dfs(int u, int f, int& c, int st) {
 50     if(++cnt[hash[u]] == 1) c++;
 51     dif[st][u] = c;
 52     for(int p = head[u]; ~p; p = next[p]) {
 53         int &v = to[p];
 54         if(v == f) continue;
 55         dfs(v, u, c, st);
 56     }
 57     if(--cnt[hash[u]] == 0) c--;
 58 }
 59 //排列组合
 60 double per[MAXN];
 61 void initPer(int n = 300) {
 62     per[0] = log(1);
 63     for(int i = 1; i <= n; ++i)
 64         per[i] = per[i - 1] + log(i);
 65 }
 66 double c(int n, int k) {
 67     return per[n] - per[n - k] - per[k];
 68 }
 69 
 70 double calc(int a, int b) {
 71     int cnt = 2;
 72     for(int i = 1; i <= n; ++i) {
 73         if(i == a || i == b) continue;
 74         if(mat[i][a] > mat[a][b] || mat[i][b] > mat[a][b]) continue;
 75         if(mat[i][b] == mat[a][b] && i < a) continue;
 76         if(mat[i][a] == mat[a][b] && i < b) continue;
 77         cnt++;
 78     }
 79     if(cnt >= k) return dif[a][b] * exp(c(cnt - 2, k - 2) - c(n, k));
 80     else return 0;
 81 }
 82 
 83 double solve() {
 84     if(n < k || k == 0) return 0;
 85     if(k == 1) return 0;
 86     double res = 0;
 87     for(int i = 1; i <= n; ++i) {
 88         for(int j = i + 1; j <= n; ++j) {
 89             res += calc(i, j);
 90         }
 91     }
 92     return res;
 93 }
 94 
 95 int main() {
 96     initPer();
 97     scanf("%d", &T);
 98     while(T--) {
 99         scanf("%d%d", &n, &k);
100         init();
101         for(int i = 1, a, b; i < n; ++i) {
102             scanf("%d%d", &a, &b);
103             add_edge(a, b);
104         }
105         for(int i = 1; i <= n; ++i) bfs(i);
106 
107         for(int i = 1; i <= n; ++i) scanf("%d", &hash[i]);
108         int cnt = 0;
109         for(int i = 1; i <= n; ++i) tmp[cnt++] = hash[i];
110         sort(tmp, tmp + cnt);
111         cnt = unique(tmp, tmp + cnt) - tmp;
112         for(int i = 1; i <= n; ++i) hash[i] = lower_bound(tmp, tmp + cnt, hash[i]) - tmp;
113         for(int i = 1, c = 0; i <= n; ++i) dfs(i, 0, c, i);
114 
115         printf("%.10f\n", solve());
116     }
117 }
View Code

 

posted @ 2014-08-02 22:44  Oyking  阅读(447)  评论(0编辑  收藏  举报