option=com_onlinejudge&Itemid=8&page=show_problem&category=514&problem=4136&mosmsg=Submission+received+with+ID+13952351" style="">题目连接: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(期望+哈希+记忆化)的更多相关文章

  1. 【bzoj1415】【聪聪和可可】期望dp(记忆化搜索)+最短路

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=57148470 Descrition 首先很明显是 ...

  2. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  3. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  4. UVA 11884 A Shooting Game(记忆化搜索)

    A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...

  5. UVA 11762 Race to 1(记忆化+期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...

  6. UVA 1390 Interconnect

    https://vjudge.net/problem/UVA-1390 题意: 给出n个点m条边的无向图, 每次随机加一条非自环的边,(加完后可出现重边), 添加每条边的概率是相等的 求使图连通的期望 ...

  7. uva 10599 - Robots(II) (dp | 记忆化搜索)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  8. UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机

    /** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...

  9. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...

随机推荐

  1. A - Oil Deposits(搜索)

    搜索都不熟练,所以把以前写的一道搜索复习下,然后下一步整理搜索和图论和不互质的中国剩余定理的题 Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp ...

  2. aspx.cs上传文件

    aspx.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  3. media_root以及static_root配置

    # At the top of settings/base.pyfrom os.path import join, abspath, dirnamehere = lambda *x: join(abs ...

  4. Windows Phone 8初学者开发—第16部分:使用应用程序栏

    原文 Windows Phone 8初学者开发—第16部分:使用应用程序栏 原文地址:  http://channel9.msdn.com/Series/Windows-Phone-8-Develop ...

  5. MYSQL设计优化

    本文将从各方面介绍优化mysql设计的一些方式. 1.优化sql语句 (1)定位须要优化的sql语句 1)show status统计SQL语句频率 对Myisam和Innodb存储引擎都计数的參数: ...

  6. 全栈project师的悲与欢

    从小米辞职出来创业的两个多月里,通过猎头或自己投简历,先后面试了知乎,今日头条,豌豆荚,美团,百度,App Annie,去哪儿,滴滴打车等技术团队,一二面(技术面)差点儿都轻松的过了,三面却没有毕业那 ...

  7. ZOJ 3603字符串操作

    解题思路:找到公共子串然后升序输出 坑的地方就在于输入是存在相同字母的 #include <stdio.h> #include <algorithm> #include < ...

  8. iTextSharp

    iTextSharp 116毫秒处理6G的文件   前言: 有一家印刷企业专为米兰新娘,微微新娘,金夫人这样的影楼印刷婚纱相册.通过一个B2B销售终端软件,把影楼的相片上传到印刷公司的服务器,服务器对 ...

  9. Solr部署详解

    Solr部署详解 时间:2013-11-24 方式:转载 目录 1 solr概述 1.1 solr的简介 1.2 solr的特点 2 Solr安装 2.1 安装JDK 2.2 安装Tomcat 2.3 ...

  10. 基于visual Studio2013解决C语言竞赛题之1002字符打印

        题目 解决代码及点评 /************************************************************************/ /* ...