【bzoj 3534】 [SDOI2014] 重建
阿新 • • 發佈:2019-01-24
題意:
給一個圖,每條邊有出現概率,求這個圖恰好為一棵樹的概率。
解法:
考慮Kirchhoff矩陣的意義:
因此我們可以令鄰接矩陣中存下邊權,使得可以用Kirchhoff矩陣計數生成樹中邊權的乘積。
考慮到答案為 (u,v)/(1−p(u,v))
但是注意到
時間複雜度
/*
I will chase the meteor for you, a thousand times over.
Please wait for me, until I fade forever.
Just 'coz GEOTCBRL.
*/
#include <bits/stdc++.h>
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<int , int>
#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<int>
#define rdll RD<long long>
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;
}