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 ...
随机推荐
- 查看一个int数组里边的每个数字出现过几次
public void aa() { int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1 }; Hashtable ht = new Hashtable(); for (int ...
- BZOJ 4034: [HAOI2015]T2( 树链剖分 )
树链剖分...子树的树链剖分序必定是一段区间 , 先记录一下就好了 ------------------------------------------------------------------ ...
- Python之路Day8
摘要: Socket编程 异常处理 线程.进程 1.socket编程 1.1 socket 三次握手,注意阻塞的应用. 1.2 socketserver(2.x写作:SocketServer) 实现多 ...
- CodeForces 260A Adding Digits
这道题目的意思是给你提供a, b, n 三个数 a为 输入的数字 ,你需要在a后面加n次 ,每次可以加0-9 但要保证每次加上去的那个数字能被b整除 不过数据规模有点大,用搜索会MLE(即使开了个开栈 ...
- HDU 3917 最大权闭合图 求最小割
具体参考http://blog.csdn.net/power721/article/details/6665750 TODO //#pragma comment(linker, "/STAC ...
- Tcl语言笔记之一
1,一个TCL脚本可以包含一个或多个命令.命令之间必须用换行符或分号隔开 2,置换 substitution %set y x+100 // ...
- 基于visual Studio2013解决算法导论之016查找最大值最小值
题目 查找最大.最小值 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...
- 05-OC多态
目录: 一.继承的缺陷 二.为什么使用继承 三.组合和聚合 四.多态 回到顶部 一.继承的缺陷 1 提高了程序的复杂度,维护性和扩展性低 2 破坏了类的封装性 回到顶部 二.为什么使用继承 1 代码复 ...
- 新鲜出炉的Using Qt 3D to visualize music
http://blog.qt.io/blog/2016/01/27/using-qt-3d-visualize-music/
- 无法从“const char *”转换为“char *”
写了如下的一段代码: const char *str; char *p=str; 提示错误: const char* 不能用于初始化char *类型的实体.这是为什么?我想应该是const char ...