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 ...
随机推荐
- 非root不能gdb attach的限制
Could not attach to process. If your uid matches the uid of the targetprocess, check the setting of ...
- 【QT相关】对话框相关
为行编辑器限制规则: QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); lineEdit->setValidator(new QRegEx ...
- POJ 1142 质因数分解
只要很朴素的分解就可以了,数据量不大 #include <stdio.h> #include <string.h> #include <stdlib.h> #inc ...
- eclipse 找不到application选项
处理如下:Window-Preferences-Run/Debug-Perspectives 中的 And Build修改为如下
- 基于visual Studio2013解决C语言竞赛题之0603打印素数
题目
- WPF-22:WPF绘制五角星改进版(增加半个五角星的绘制)-修改bug
之前用坐标画多边形的方法,绘制五角星.今天调试时发现当时写的时候有bug,修改一下. 原文: http://blog.csdn.net/yysyangyangyangshan/article/deta ...
- Android 涂鸦最佳实践
Android中实现手势画图一般都两种方式,一是直接在View上绘制,而是使用SurfaceView. 两者还是有一些差别的.简介下. View:显示视图,内置画布,提供图形绘制函数.触屏事件.按键事 ...
- libvirt学习
高级libvirt API可划分为5个API部分:虚拟机监控程序连接API.域API.网络API.存储卷API.存储池API.
- android 应用静默自启动的解决方法
一个apk完全的自动静默启动目前不能实现,所以就用到了Activity的一个方法activity.moveTaskToBack(boolean),这个方法就是可以退出应用到后台而不是finish()退 ...
- c语言,链表
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> struct Node{ struct ...