-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathUVa1390.cc
76 lines (73 loc) · 1.7 KB
/
UVa1390.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// UVa1390 Interconnect
// 陈锋
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
typedef long long LL;
const int MAXN = 32;
int N, M, G[MAXN][MAXN], VIS[MAXN];
int dfs(int u) {
int ans = 1;
VIS[u] = 1;
_for(v, 0, N) if (G[u][v] && !VIS[v]) ans += dfs(v);
return ans;
}
unordered_map<string, double> D;
double dp(const string& s) {
if (D.count(s)) return D[s];
double& d = D[s];
int k = s.size();
if (k == 1) {
assert(s[0] == N);
return d = 0;
}
double e = 0, p = 0;
_for(i, 0, k) {
int ci = s[i];
_for(j, i + 1, k) {
int cj = s[j];
auto s2 = s;
s2[i] = ci + cj, s2.erase(s2.begin() + j);
sort(begin(s2), end(s2));
e += ci * cj * 2 * (dp(s2) + 1), p += ci * cj * 2;
}
}
e /= (N * (N - 1)), p /= N * (N - 1);
// d = e + (1+d)*(1-p) = e + 1-p + (1-p)d -> d = e/p + (1-p)/p
return d = e / p + (1 - p) / p;
}
int main() {
while (scanf("%d%d", &N, &M) == 2) {
memset(G, 0, sizeof(G)), memset(VIS, 0, sizeof(VIS)), D.clear();
int u, v;
_for(i, 0, M) {
scanf("%d%d", &u, &v);
--u, --v;
G[u][v] = G[v][u] = 1;
}
string s;
_for(u, 0, N) if (!VIS[u]) s.push_back((char)dfs(u));
printf("%.6lf\n", dp(s));
}
return 0;
}
// 2211999 3710 Interconnect Accepted C++11 0.206
// 2017-07-22 13:58:36