uva 1390 - Interconnect(期望+哈希+记忆化)
题目大意:给出n表示有n个点,m表示有m条边,如今任选两点建立一条边。直到整个图联通,问说还需建立边数的期望,建过边的两点仍能够建边。
解题思路:哈希的方法非常是巧妙。将各个联通分量中节点的个数c[i]转换成一个30进制的数(由于节点个数最多为30)。由于结果非常大。所以对1e5+7取模。获得的哈希值作为插入和搜索的起点。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 30;
const int mod = 1e5+7;
struct state {
int c[maxn], flag;
double val;
void clear () { memset(c, 0, sizeof(c)); }
int hash() {
int x = 0;
for (int i = 0; i < maxn; i++)
x = (x * 30 + c[i]) % mod;
return x;
}
bool operator == (const state& u) {
for (int i = 0; i < maxn; i++)
if (c[i] != u.c[i])
return false;
return true;
}
bool operator != (const state& u) {
return !(*this == u);
}
}start, ha[mod+7];
int n, m, f[maxn+5], s[maxn+5];
double dive;
int getfar (int x) {
return f[x] == x ? x : f[x] = getfar(f[x]);
}
void link (int x, int y) {
int p = getfar(x);
int q = getfar(y);
if (p == q)
return;
f[q] = p;
s[p] += s[q];
}
void inserthash (state u) {
int x = u.hash();
while (ha[x].flag) {
if (++x == mod)
x = 0;
}
ha[x] = u;
ha[x].flag = 1;
}
double gethash (state u) {
int x = u.hash();
while (ha[x].flag && ha[x] != u) {
if (++x == mod)
x = 0;
}
return ha[x] == u ? ha[x].val : -1;
}
void init () {
dive = n * (n - 1) / 2.0;
start.clear();
for (int i = 0; i <= n; i++) {
s[i] = 1;
f[i] = i;
}
for (int i = 0; i < mod; i++)
ha[i].flag = 0;
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
link(a, b);
}
for (int i = 1; i <= n; i++) {
if (f[i] == i)
start.c[i-1] = s[i];
}
}
double solve (state u) {
sort(u.c, u.c+maxn);
if (u.hash() == n)
return 0;
double x = gethash(u);
if (x != -1.0)
return x;
double ans = 0, tmp = 0;
for (int i = 0; i < maxn; i++)
tmp += u.c[i] * (u.c[i] - 1) / 2.0;
for (int i = 0; i < maxn; i++) {
if (u.c[i] == 0)
continue;
for (int j = i+1; j < maxn; j++) {
if (u.c[j] == 0)
continue;
state v = u;
v.c[i] += v.c[j];
v.c[j] = 0;
ans += u.c[i] * u.c[j] * solve(v);
}
}
ans /= dive;
ans++;
ans /= (1 - tmp / dive);
u.val = ans;
inserthash(u);
return ans;
}
int main () {
while (scanf("%d%d", &n, &m) == 2) {
init();
printf("%.10lf\n", solve(start));
}
return 0;
}
uva 1390 - Interconnect(期望+哈希+记忆化)的更多相关文章
- 【bzoj1415】【聪聪和可可】期望dp(记忆化搜索)+最短路
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=57148470 Descrition 首先很明显是 ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- UVA 11884 A Shooting Game(记忆化搜索)
A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...
- UVA 11762 Race to 1(记忆化+期望)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...
- UVA 1390 Interconnect
https://vjudge.net/problem/UVA-1390 题意: 给出n个点m条边的无向图, 每次随机加一条非自环的边,(加完后可出现重边), 添加每条边的概率是相等的 求使图连通的期望 ...
- uva 10599 - Robots(II) (dp | 记忆化搜索)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机
/** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...
随机推荐
- sqlserver 三种恢复模式
sql server数据库提供了三种恢复模式:完整,简单和大容量日志,这些模式决定了sql server如何使用事务日志,如何选择它要记录的操作,以及是否截断日志.截断事务日志是删除己执行事务并把该日 ...
- error C2664: “LoadLibraryW”: 不能将参数 1 从“const char *”转换为“LPCWSTR”
在使用VS2010编写运行时动态链接dll文件时出现的一个问题,问题解决得益于此文章: http://blog.sina.com.cn/s/blog_6a2236590100xbgl.html 通过调 ...
- A*算法(八数码问题)
#include <iostream> #include <cstring> #include <vector> #include <cmath> #i ...
- 10994 - Simple Addition(规律)
Problem E Simple Addition Input: Standard Input Output: Standard Output Let’s define a simple recurs ...
- Ural 1197 - Lonesome Knight
The statement of this problem is very simple: you are to determine how many squares of the chessboar ...
- 引用 移植Linux到s3c2410上
引用 bsky 的 移植Linux到s3c2410上来源:http://www.embed.com.cn/downcenter/Article/Catalog12/4000.htm 移植Linux到s ...
- pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。
http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...
- 错误解决--oracle中出现ORA-01791: 不是 SELECTed 表达式 错误
Oracle数据库,执行下面语句出现错误“ORA-01791: 不是 SELECTed 表达式”: select distinct t.name from auth_employee t order ...
- vc中关于 directx的配置,和dxsdk_extras(directshow)
2009-11-10 0:28 此文章:自己编写 转载于<汤姆&杰瑞> DirectShow 1 -- 下载与VC配置 1 DirectX SDK9 Directshow sd ...
- Qt 释放新建窗口资源
当Widget *w = new Widget(); 不方便调用 delete w;时 Widget *w = new Widget(); w->setAttribute(Qt::WA_Dele ...