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 ...
随机推荐
- 【Eclipse】Tomcat 一直处于starting状态,项目却已成功启动
是因为Eclipse里面设置了代理.preference-network connections-activity provider-direct-应用.重启tomcat即可.
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- BZOJ 1977: [BeiJing2010组队]次小生成树 Tree( MST + 树链剖分 + RMQ )
做一次MST, 枚举不在最小生成树上的每一条边(u,v), 然后加上这条边, 删掉(u,v)上的最大边(或严格次大边), 更新答案. 树链剖分然后ST维护最大值和严格次大值..倍增也是可以的... - ...
- ms sql 获取字符串首字母
很久没有编写新文章,现在发布一篇自定义函数,针对于ms sql数据库中需要获取字符串首字母,对于需要的朋友希望对你有用,如果你有更好的方法也可以给我留言.函数如下: --获取字符串首字母 CREATE ...
- [转]Mysql自动备份并保存近15天记录脚本
本脚本主要现实在CentOS中实现对MySQL数据库的备份和保留最近十五天的备份文件.避免太多无用陈旧的备份占用空间. #!/bin/bash id='root' #用户名 pwd='123123' ...
- javascript面向对象创建高级 Web 应用程序
目录 JavaScript 对象是词典 JavaScript 函数是最棒的 构造函数而不是类 原型 静态属性和方法 闭包 模拟私有属性 从类继承 模拟命名空间 应当这样编写 JavaScript ...
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
安装 1准备编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 2 下载源码包(由于goog ...
- cglib源码学习交流
背景 前段时间在工作中,包括一些代码阅读过程中,spring aop经常性的会看到cglib中的相关内容,包括BeanCopier,BulkBean,Enancher等内容,以前虽大致知道一些内容,原 ...
- Domain(AD) 管理
新建账户 文件夹权限管理
- hdu 1166 敌兵布阵(线段树,树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1166 直接线段树模板 AC代码: #include<iostream> #include<cs ...