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. SB中使用Autolayout设置到父视图的间距为0

    发现打上钩的话在plus以下的屏幕上控件的实际x会是-4但是在plus上是0, 所以要设置一致的到视图的左右都未0 必须把Constrain to margins钩去掉!

  2. Linux内核态抢占机制分析(转)

    Linux内核态抢占机制分析  http://blog.sina.com.cn/s/blog_502c8cc401012pxj.html 摘 要]本文首先介绍非抢占式内核(Non-Preemptive ...

  3. iOS开发之视图控制器(UIViewController)

    视图控制器应该在MVC设计模式中扮演控制层(C)的角色,UIViewController的职责对内管理与之关联的View,对外跟其他UIViewController通信和协调.一个视图控制器管理一个视 ...

  4. 一个库搞定各种分享--ShareSDK

    ShareSDK是为iOS.Android.WindowsPhone提供社会功能的一个组件,开发者只需10分钟即可集成到自己的APP中,它不仅支持分享给QQ好友.微信好友.微信朋友圈.新浪微博.腾迅微 ...

  5. qt5_qml_Opengl_shader 第一弹----------------------openglunderqml的简化及介绍

    最近得知opengl可以通过纹理贴图来渲染yuv的数据,这就免去了yuv-rgb,这个过程在arm上还是很耗时间的,于是就接触了opengl. 写这篇文章的目的是方便初学者使用qml来调用opengl ...

  6. 分享一个 CSDN 自动评论角本

    先修改帐户名和密码 import requests from BeautifulSoup import BeautifulSoup import time def commitfunc(source_ ...

  7. 华为HCNA教程(笔记)

    第一章 VRP操作基础 1VRP基础 MiniUsb串口连接交换机的方法 2eNSP入门 3命令行基础(1) eNSP中路由开启后(记住port)---第三方软件连接该路由方法:telnet 127. ...

  8. sparkUI使用与扩展

      http://www.jianshu.com/p/8e4c38d0c44e

  9. Android开发--CardView使用

    Android5.0中向我们介绍了一个全新的控件–CardView,从本质上看,可以将CardView看做是FrameLayout在自身之上添加了圆角和阴影效果.请注意:CardView被包装为一种布 ...

  10. 在Windows下用Eclipse+CDT+MinGW搭建C++开发平台

    本文提供了在Windows下用Eclipse+CDT+MinGW搭建C / C++开发平台的方法, 测试平台为Windows XP Sp2 CHS.   以下软件均为Windows平台下的版本. 1. ...