HDU

来源:互联网 发布:神舟软件科技有限公司 编辑:程序博客网 时间:2024/06/08 16:41

Soda has a bipartite graph with nn vertices and mm undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges he can add.

Note: There must be at most one edge between any pair of vertices both in the new graph and old graph.
Input
There are multiple test cases. The first line of input contains an integer TT (1≤T≤100)(1≤T≤100), indicating the number of test cases. For each test case:

The first line contains two integers nn and mm, (2≤n≤10000,0≤m≤100000)(2≤n≤10000,0≤m≤100000).

Each of the next mm lines contains two integer u,vu,v (1≤u,v≤n,v≠u)(1≤u,v≤n,v≠u) which means there’s an undirected edge between vertex uu and vertex vv.

There’s at most one edge between any pair of vertices. Most test cases are small.
Output
For each test case, output the maximum number of edges Soda can add.
Sample Input
2
4 2
1 2
2 3
4 4
1 2
1 4
2 3
3 4
Sample Output
2
0

题意

n个房间,每个房间都有若干个钥匙打开其他的门,如果手上没有钥匙可以选择等概率随机选择一个门炸开,求用炸弹数的期望。

思路

对于门u 如果知道有k个门被打开就能打开u 那么炸一次就能打开u的概率为 k/n 进而 u被打开的期望 就是n/k .
由于期望的可加性
打开所有门需要炸弹数的期望就是 sigma(n/k)/n = sigme(1/k)
用bitset优化

疑问点1:为什么【炸一次就能打开u的概率为 k/n 进而 u被打开需要的炸弹数期望 就是n/k .】
2:期望可加性。。最后/n 也不懂。。。。。

百度了一万发题解还是不懂。。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <bitset>using namespace std;bitset<1005> b[1005];int n,m,v;int main(){    freopen("in.txt","r",stdin);    int T;    scanf("%d",&T);    int cas=0;    while(T--){        scanf("%d",&n);        for(int i=0;i<=n;i++) b[i].reset();        for(int i=0;i<=n;i++) b[i][i]=1;        for(int i=0;i<n;i++){            scanf("%d",&m);            while(m--){                scanf("%d",&v);                v--;                b[i][v]=1;            }        }        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)                if(b[j][i]) b[j]=b[j] | b[i];        double ans=0.0;        for(int i=0;i<n;i++)        {            int cnt=0;            for(int j=0;j<n;j++)                if(b[j][i]) cnt++;            ans+=1.0/cnt;        }        printf("Case #%d: %.5lf\n",++cas,ans);    }    return 0;}
原创粉丝点击