Blackops

初心易得,始终难守

0%

SPOJ HIGH(生成树计数,高斯消元求行列式)

HIGH - Highways
no tags

In some countries building highways takes a lot of time… Maybe that’s because there are many possiblities to construct a network of highways and engineers can’t make up their minds which one to choose. Suppose we have a list of cities that can be connected directly. Your task is to count how many ways there are to build such a network that between every two cities there exists exactly one path. Two networks differ if there are two cities that are connected directly in the first case and aren’t in the second case. At most one highway connects two cities. No highway connects a city to itself. Highways are two-way.

Input

The input begins with the integer t, the number of test cases (equal to about 1000). Then t test cases follow. The first line of each test case contains two integers, the number of cities (1<=n<=12) and the number of direct connections between them. Each next line contains two integers a and b, which are numbers of cities that can be connected. Cities are numbered from 1 to n. Consecutive test cases are separated with one blank line.

Output

The number of ways to build the network, for every test case in a separate line. Assume that when there is only one city, the answer should be 1. The answer will fit in a signed 64-bit integer.

Example

Sample input:
4
4 5
3 4
4 2
2 3
1 2
1 3

2 1
2 1

1 0

3 3
1 2
2 3
3 1

Sample output:
8
1
1
3

题目链接:SPOJ HIGH

矩阵树定理,用D矩阵和A矩阵作差得到G,然后求G的任意一个$n-1$阶矩阵行列式,其中用到高斯消元
代码:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 15;
double Mat[N][N];

void init()
{
CLR(Mat, 0);
}
double Gaussian(int ne, int nv)
{
int i, j;
double ans = 1;
for (int ce = 1, cv = 1; ce <= ne && cv <= nv; ++ce, ++cv)
{
int te = ce;
for (i = ce + 1; i <= ne; ++i)
if (fabs(Mat[i][cv]) > fabs(Mat[ce][cv]))
te = ce;
if (Mat[te][cv] == 0)
return 0;
if (te != ce)
{
for (i = cv; i <= nv; ++i)
swap(Mat[ce][i], Mat[te][i]);
ans *= -1;
}
ans *= Mat[ce][cv];
for (j = cv + 1; j <= nv; ++j)
Mat[ce][j] /= Mat[ce][cv];
for (i = ce + 1; i <= ne; ++i)
for (j = cv + 1; j <= nv; ++j)
Mat[i][j] -= Mat[i][cv] * Mat[ce][j];
}
return ans;
}
int main(void)
{
int T;
int n, m, u, v, i;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d%d", &n, &m);
for (i = 0; i < m; ++i)
{
scanf("%d%d", &u, &v);
++Mat[u][u];
++Mat[v][v];
Mat[u][v] = -1;
Mat[v][u] = -1;
}
printf("%.0f\n", Gaussian(n - 1, n - 1));
}
return 0;
}