Description:
Each edge has a probability of being present, asking the probability of the occurrence of a spanning tree.
Solution:
The probability that the probability of existence on the spanning tree does not exist by other edges.
Matrix tree theorem can find the sum of the product of each spanning tree, then we turn the Benquan into Pi1−pi p i 1−p i \frac{p_i}{1-p_i}, make The Matrix tree theorem, and finally multiply ∏1−pi∏1−p i \prod{1-p_i}.
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std;
const int MAXN = 55;
Const double EPS = 1e-8;
int n;
Double TMP = 1.0;
Double A[MAXN][MAXN];
Double det (int n) {for (int i = 1; I <= n; ++i) {int p = i;
for (int j = i + 1; j <= N; ++j) {if (Fabs (A[j][i]) > Fabs (A[p][i])) {p = j;
}} for (int j = 1; j <= N; ++j) {swap (a[i][j], a[p][j]);
} if (Fabs (A[i][i]) < EPS) {return 0.0;
} for (int j = i + 1; j <= N; ++j) {Double t = a[j][i]/a[i][i];
for (int k = i; k <= N; ++k) {a[j][k]-= t * a[i][k];
}}} double ret = 1.0;
for (int i = 1; I <= n; ++i) {ret *= a[i][i];
} return ret;
} int main () {scanf ("%d", &n); for (int i = 1, i <= N; ++i) {for (int j = 1; J <= N;
++J) {scanf ("%lf", &a[i][j]);
if (i! = j) {if (A[i][j] > 1.0-eps) {a[i][j]-= EPS;
} if (I < j) {TMP = TMP * (1.0-a[i][j]);
} A[i][j]/= 1.0-a[i][j];
}}} for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) {if (i! = j) {
A[i][i] + = a[i][j];
A[I][J] =-a[i][j];
}}} printf ("%.10f\n", det (n-1) * tmp);
return 0; }