-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmatrix_tree.cpp
43 lines (42 loc) · 1.04 KB
/
matrix_tree.cpp
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
#define zero(x) ((x>0? x:-x)<1e-15)
const int N = 100;
double a[N][N];
double b[N][N];
int g[110][110];
double det(double a[][N],int n) {
int i, j, k, sign = 0;
double ret = 1, t;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
b[i][j] = a[i][j];
for (i = 0; i < n; i++) {
if (zero(b[i][i])) {
for (j = i + 1; j < n; j++)
if (!zero(b[j][i]))
break;
if (j == n)
return 0;
for (k = i; k < n; k++)
swap(b[i][k], b[j][k]);
sign++;
}
ret *= b[i][i];
for (k = i + 1; k < n; k++)
b[i][k] /= b[i][i];
for (j = i + 1; j < n; j++)
for (k = i + 1; k < n; k++)
b[j][k] -= b[j][i] * b[i][k];
}
if (sign & 1)
ret = -ret;
return ret;
}
int main() {
/* 0 based
* a[i][i] = degree[i];
* if(g[i][j]) {
* a[i][j]--;
* }
* ans = det(a, n - 1);
*/
}