#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector > #include <queue> #include <map> #include <set> #include <algorithm> #include <ctime># pragma comment (linker, "/stack:102400000,102400000") using namespace std; #define EPS 1e-10#define N 500020#define B 20# Define M 3000020#define inf 0x3f3f3f3f#define LL long long#define PII pair<int, int> #define MP make_pair#define fi F Irst#define se second#define mod 1000007#define MD (LL + RR >> 1) struct State {int a[21], cnt;bool operator < (CO NST State &b) const {return CNT < b.cnt;}} St[n];int f[1000007];int tot;double p[n];int n;vector<int> s[n];int b[n];d ouble dp[21][700];d ouble g[22][22], X[ 22];int tr[700][22][22];void dfs (int k, int mx, int sum) {if (sum = = N) {++tot;for (int i = 1; i < K; ++i) {St[tot].a[i] = B[i];} st[tot].cnt = K-1;return;} for (int i = mx; i <= n-sum; ++i) {if (! ( N-sum-i = = 0 | | N-sum-i >= i))Continue;b[k] = I;dfs (k + 1, I, sum + i);}} int code (state &t) {int ret = t.cnt;for (int i = 1; I <= t.cnt; ++i) {ret = 1LL * RET * 233% mod + t.a[i];ret%= mo D;} return ret;} int merge (State t, int x, int y) {t.a[x] + = T.a[y];swap (T.a[y], t.a[t.cnt]); T.cnt--;sort (t.a + 1, t.a + t.cnt + 1); return F[code (t)];} int SGN (double x) {if (Fabs (x) < EPS) return 0;return x < 0? -1:1;} void Gauss () {for (int i = 1; I <= n; ++i) {int u = -1;for (int v = i; v <= N; ++v) {if (U = =-1 | | fabs (g[v][i)) > Fabs (G[u][i])) u = V;} if (U = =-1 | | sgn (g[u][i]) = = 0) {puts ("error"); exit (0);} for (int x = i; x <= n + 1; ++x) swap (g[i][x], g[u][x]), for (int x = i + 1; x <= n; ++x) {if (SGN (G[x][i])) {double tmp = G[x][i]/g[i][i];for (int k = i; k <= n + 1; ++k) {g[x][k]-= g[i][k] * TMP;}}} for (int i = n; i >= 1; i) {x[i] = g[i][n+1];for (int j = i + 1; j <= N; ++j) x[i]-= g[i][j] * X[j]; X[i]/= g[i][i];} void Get_data () {freopen ("Tt.txt", "w", stdout); int t = 19;printf ("1\n%d\n", T); for (int i = 1; I <= t; ++i) printf ("%.3LF", 0.5), and for (int i = 1; I <= t; ++i) {printf ("%d", t); for (int j = 1; J <= T; ++J) printf ("%d", j);p UTS ("");} Exit (0);} int main () {//get_data ();//freopen ("Tt.txt", "R", stdin), int cas, KK = 0;scanf ("%d", &cas), while (cas--) {scanf ("%d", &n); for (int i = 1; I <= n; ++i) scanf ("%lf", &p[i]), S[i].clear (); for (int i = 1; I <= n; ++i) {int T, X;scan F ("%d", &t), while (t--) {scanf ("%d", &x); S[i].push_back (x);}} tot = 0;dfs (1, 1, 0); sort (st + 1, st + tot + 1); for (int i = 1; I <= tot; ++i) {F[code (st[i])] = i;} for (int i = 1, i <= N; ++i) dp[i][1] = 0;for (int i = 2; I <= tot; ++i) {for (int x = 1; x <= st[i].cnt; ++x) {for ( int y = x + 1; Y <= st[i].cnt; ++y) {Tr[i][x][y] = merge (St[i], x, y);}}} for (int i = 2; l <= tot; ++i) {memset (g, 0, sizeof g); for (int u = 1; u <= n; ++u) {Double right = 1;for (int x = 1; x <= st[i].cnt; ++x) {for (int y = x + 1; y <= st[i].cnt; ++y) {double TMP = st[i].a[x] * st[I].A[Y];TMP = tmp/(n * (n-1)/2)/s[u].size () * P[u];int id = tr[i][x][y];for (int zz = 0; ZZ < s[u].size (); ++zz) {int z = s[u][zz];right + = dp[z][id] * tmp;//if (st[id].cnt = = 1) Right-= tmp;}}} G[U][N+1] = right;g[u][u] = 1;double tmp = 0;for (int x = 1; x <= st[i].cnt; ++x) tmp + = st[i].a[x] * (St[i].a[x]-1)/ 2;TMP = tmp * P[u]/(n * (n-1)/2)/s[u].size (); for (int x = 0; x < s[u].size (); ++x) {int v = s[u][x];g[u][v]-= TMP;} TMP = (1-p[u])/s[u].size (); for (int x = 0; x < s[u].size (); ++x) {int v = s[u][x];g[u][v] = tmp;}} Gauss (); for (int j = 1; j <= N; ++j) dp[j][i] = X[j];} printf ("Case #%d:%.6f\n", ++kk, Dp[1][tot]);} return 0;}
Hdu 4936 Rainbow Island (pressure dp+ Gaussian elimination element)