HDU - 5036 Explosion

Problem Description

Everyone knows Matt enjoys playing games very much. Now, he is playing such a game. There are N rooms, each with one door. There are some keys(could be none) in each room corresponding to some doors among these N doors. Every key can open only one door. Matt
has some bombs, each of which can destroy a door. He will uniformly choose a door that can not be opened with the keys in his hand to destroy when there are no doors that can be opened with keys in his hand. Now, he wants to ask you, what is the expected number
of bombs he will use to open or destroy all the doors. Rooms are numbered from 1 to N.

Input

The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

In the first line of each test case, there is an integer N (N<=1000) indicating the number of rooms.

The following N lines corresponde to the rooms from 1 to N. Each line begins with an integer k (0<=k<=N) indicating the number of keys behind the door. Then k integers follow corresponding to the rooms these keys can open.

Output

For each test case, output one line "Case #x: y", where x is the case number (starting from 1), y is the answer which should be rounded to 5 decimal places.

Sample Input

2
3
1 2
1 3
1 1
3
0
0
0

Sample Output

Case #1: 1.00000
Case #2: 3.00000

Source

field=problem&key=2014+ACM%2FICPC+Asia+Regional+Beijing+Online&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">2014 ACM/ICPC Asia Regional Beijing Online

题意:n个房间,每一个房间都有若干个钥匙打开其它的门,假设手上没有钥匙能够选择等概率随机选择一个门炸开。求用炸弹数的期望。

思路:每一个房间期望都是可加的。

单独考虑一个房间,假设有k个房间被炸开都会导致这个房间被打开。

那么炸一次这个房间被打开的概率即为kn。也就意味着为了把这个房间打开的期望炸的次数为nk。所有加起来后除以n就可以。bitset优化。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
const int maxn = 1005;

bitset<maxn> a[maxn];

int main() {
	int t, cas = 1;
	int n;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			a[i].reset();
			a[i][i] = 1;
		}

		int c, x;
		for (int i = 0; i < n; i++) {
			scanf("%d", &c);
			while (c--) {
				scanf("%d", &x);
				a[i][--x] = 1;
			}
		}

		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (a[j][i])
					a[j] |= a[i];

		double ans = 0;
		for (int i = 0; i < n; i++) {
			c = 0;
			for (int j = 0; j < n; j++)
				if (a[j][i])
					c++;
			ans += 1.0 / c;
		}

		printf("Case #%d: %.5lf\n",cas++,ans);
	}
	return 0;
}
时间: 2024-08-06 19:59:40

HDU - 5036 Explosion的相关文章

HDU 5036 Explosion(北京网络赛E题)

HDU 5036 Explosion 题目链接 思路:对于每个点,只要考虑哪些炸掉能到他的个数cnt,那么他对应的期望就是1 / cnt,然后所以期望的和就是答案,用bitset来维护 代码: #include <cstdio> #include <cstring> #include <bitset> using namespace std; const int N = 1005; int t, n; bitset<N> bs[N]; int main()

hdu 5036 Explosion bitset优化floyd

http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么1能联通的顶点就可以直接走过去,其他不和1连通的,就需要炸坏.问需要炸弹的期望. 比如一副图是1-->2-->3的.那么期望是11 / 6 假如从1号点开始,1/3概率选中1号点开始,那么需要炸弹数是1(炸开一号),贡献是1/3 假如从2号点开始,1/3概率选中2号点开始,那么需要炸开2号点,然后

hdu 5036 Explosion (bitset优化的传递闭包求解概率)

Explosion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 25 Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a g

hdu 5036 Explosion(概率期望+bitset)

Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a game. There are N rooms, each with one door. There are some keys(could be none) in each room corresponding to some doors among these N doors. Every key

HDU 5036 Explosion (传递闭包+bitset优化)

<题目链接> 题目大意: 一个人要打开或者用炸弹砸开所有的门,每个门后面有一些钥匙,一个钥匙对应一个门,告诉每个门里面有哪些门的钥匙.如果要打开所有的门,问需要用的炸弹数量为多少. 解题分析:因为许多门和他们之后的钥匙可能形成闭包的关系,所以,对于所有的闭包而言,只需要炸毁其中的一个门,就可以用其后面的钥匙打开闭包中至少一扇另外的门,一次类推.所以,假设闭包中包含$num$扇门,用炸弹打开闭包中任意一扇门的概率就为:$1/num$(因为炸毁每个闭包的概率为1,即每个闭包必然需要一枚炸弹).所有

【bitset模板题】HDU 5036 Explosion

题意: 一个人要打开或者用炸弹砸开所有的门,每个门里面有一些钥匙,一个钥匙对应一个门,有了一个门的钥匙就能打开相应的门,告诉每个门里面有哪些门的钥匙,问用的炸弹为期望值. 分析: 期望值 = 每个门用炸弹炸开的概率之和 而 每个门用炸弹炸开的概率 = 1 / 到达这个门的方案数, 因为炸开门的方案只有一种 我们用bitset记录门间的联通情况,求出方案数即可 我们开一个bitset数组 a 假如  a[i] = 0  1  0  1  1  0  1          即 i 号门能到 1 3

HDU - 5036 Operation the Sequence

Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,-,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types: Type1: O 1 call fun1(); Type2: O 2 call fun2(); Type3

HDU 3622 Bomb Game(二分+2-SAT)

Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5396    Accepted Submission(s): 1925 Problem Description Robbie is playing an interesting computer game. The game field is an unbounde

(KMP 水)Wow! Such Doge! -- hdu -- 4847

http://acm.hdu.edu.cn/showproblem.php?pid=4847 Wow! Such Doge! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4847 Description Chen, Adrian (November 7, 2013). “Doge Is An Ac- tually Good Inter