发现一个总结的很好的台湾同学的生成树学习笔记:http://www.csie.ntnu.edu.tw/~u91029/SpanningTree.html

不知道这个台湾同学是谁,但是好像极其厉害,附博客:http://www.csie.ntnu.edu.tw/~u91029/

还有这个同学编纂的<台湾师范大学ACM算法入门教程(繁体)>http://acm.nudt.edu.cn/~twcourse/

终于知道楼教主为什么去facebook了,因为他连续2011,2012两年获得Facebook Hacker Cup第三。

SPOJ-104:无向图生成树计数模板题

104. Highways

Problem code: HIGH

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 submission 7361111 (C++ 4.3.2) plaintext list.
Status: AC, problem HIGH, contest SPOJ.
By ldg859 (ldg), 2012-07-23 10:35:07.*/

#include<iostream> #include<math.h> #include<stdio.h> #include<string.h> using namespace std; #define zero(x) ((x>0? x:-x)<1e-15) int const MAXN = 100; double a[MAXN][MAXN]; double b[MAXN][MAXN]; int g[53][53]; int n,m; double det(double a[MAXN][MAXN],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++) t = b[i][k], b[i][k] = b[j][k], b[j][k] = t; 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; } void build(){ while (m--) { int a, b; scanf("%d%d", &a, &b); g[a-1][b-1]=g[b-1][a-1]=1; } } int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d%d", &n, &m); memset(g,0,sizeof(g)); build(); memset(a,0,sizeof(a)); for (int i=0;i<n;i++) { int d=0; for (int j=0;j<n;j++) if (g[i][j]) d++; a[i][i]=d; } for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (g[i][j]) a[i][j]=-1; double ans = det(a, n-1); printf("%0.0lf\n", ans); } return 0; }