题意:
给一个图,每条边有出现概率,求这个图恰好为一棵树的概率。
解法:
考虑Kirchhoff矩阵的意义: K[G]=D[G]−A[G]=B[G]B[G]T ,之所以能够进行生成树计数是对于其伴随矩阵在计数 n−1 条边的集合时,当 n−1 条边中存在环就会产生线性组合而导致行列式为零,否则恰好对角线上均为伴随矩阵中所赋的值,使得 det(Bi,j)2 就为1。
因此我们可以令邻接矩阵中存下边权,使得可以用Kirchhoff矩阵计数生成树中边权的乘积。
考虑到答案为 ∑T是一棵生成树∏u,v(p(u,v)∗[(u,v)∈T])((1−p(u,v))∗[u,v∉T]) ,我们可以令边权为 p(u,v)/(1−p(u,v)) ,求出行列式然后再乘回分母。
但是注意到 p(u,v)==1 时分母为0,此时我们理论上可以用一个足够小的 ϵ 来表示0,考虑到double的表示法,取 ϵ=2−20 就足够了,这样可以避免过大/过小数运算所导致的精度误差,而原先的概率的精度(两位小数)也是可以接受的。
时间复杂度 O(n3) ,不知道为什么出题人出这么小,可能是怕数据都是0然后被水过去吧。
/*
I will chase the meteor for you, a thousand times over.
Please wait for me, until I fade forever.
Just 'coz GEOTCBRL.
*/
#include
using namespace std;
#define fore(i,u) for (int i = head[u] ; i ; i = nxt[i])
#define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++)
#define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --)
#define For(i,a,b) for (int i = a , _ = b ; i < _ ; i ++)
#define Dwn(i,a,b) for (int i = ((int) a) - 1 , _ = (b) ; i >= _ ; i --)
#define fors(s0,s) for (int s0 = (s) , _S = s ; s0 ; s0 = (s0 - 1) & _S)
#define foreach(it,s) for (__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define mp make_pair
#define pb push_back
#define pii pair
#define fir first
#define sec second
#define MS(x,a) memset(x , (a) , sizeof (x))
#define gprintf(...) fprintf(stderr , __VA_ARGS__)
#define gout(x) std::cerr << #x << "=" << x << std::endl
#define gout1(a,i) std::cerr << #a << '[' << i << "]=" << a[i] << std::endl
#define gout2(a,i,j) std::cerr << #a << '[' << i << "][" << j << "]=" << a[i][j] << std::endl
#define garr(a,l,r,tp) rep (__it , l , r) gprintf(tp"%c" , a[__it] , " \n"[__it == _])
template <class T> inline void upmax(T&a , T b) { if (a < b) a = b ; }
template <class T> inline void upmin(T&a , T b) { if (a > b) a = b ; }
typedef long long ll;
const int maxn = 57;
const int maxm = 200007;
const int mod = 1000000007;
const int inf = 0x7fffffff;
const double eps = pow(2 , -19);
typedef int arr[maxn];
typedef int adj[maxm];
inline int fcmp(double a , double b) {
if (fabs(a - b) <= eps) return 0;
if (a < b - eps) return -1;
return 1;
}
inline int add(int a , int b) { return ((ll) a + b) % mod ; }
inline int mul(int a , int b) { return ((ll) a * b) % mod ; }
inline int dec(int a , int b) { return add(a , mod - b % mod) ; }
inline int Pow(int a , int b) {
int t = 1;
while (b) {
if (b & 1) t = mul(t , a);
a = mul(a , a) , b >>= 1;
}
return t;
}
#define rd RD
#define rdll RD
template <typename Type>
inline Type RD() {
Type x = 0;
int flag = 0;
char c = getchar();
while (!isdigit(c) && c != '-')
c = getchar();
(c == '-') ? (flag = 1) : (x = c - '0');
while (isdigit(c = getchar()))
x = x * 10 + c - '0';
return flag ? -x : x;
}
inline char rdch() {
char c = getchar();
while (!isalpha(c)) c = getchar();
return c;
}
// beginning
int n;
double to_mul , a[maxn][maxn];
void input() {
n = rd();
to_mul = 1;
rep (i , 1 , n) rep (j , 1 , n) {
scanf("%lf" , &a[i][j]);
double b = 1 - a[i][j];
upmax(b , eps);
if (i < j)
to_mul *= b;
if (i != j)
a[i][j] = - a[i][j];
a[i][j] /= b;
}
rep (i , 1 , n) rep (j , i + 1 , n)
a[i][i] -= a[i][j] , a[j][j] -= a[i][j];
}
inline double det() {
double ret = 1;
For (i , 1 , n) {
int j = i;
while (j < n && fabs(a[j][i]) < eps) j ++;
if (j == n) return 0;
if (i != j) {
ret *= -1;
For (k , 1 , n) swap(a[i][k] , a[j][k]);
}
For (j , i + 1 , n) if (fabs(a[j][i]) >= eps) {
double t = a[j][i] / a[i][i];
For (k , i , n) a[j][k] -= t * a[i][k];
}
}
For (i , 1 , n)
ret *= a[i][i];
return fabs(ret);
}
void solve() {
double ans = det();
ans *= to_mul;
printf("%.10lf\n" , ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.txt" , "r" , stdin);
#endif
input();
solve();
return 0;
}