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 就是说一个无向图,求生成树个数
矩阵树定理
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define eps 1e-9 #define N 20 int n,m; int d[N][N],c[N][N]; double a[N][N]; void gauss() { for(int i=1;i<=n;i++) { for(int j=i;j<=n+1;j++) { if(j==n+1) { printf("0\n"); return; } if(fabs(a[j][i])>eps) { for(int k=i;k<=n;k++) swap(a[j][k],a[i][k]); break; } } for(int j=i+1;j<=n;j++) { double t=a[j][i]/a[i][i]; for(int k=1;k<=n;k++) a[j][k]=a[j][k]-t*a[i][k]; } } double ans=1; for(int i=1;i<=n;i++) ans*=a[i][i]; printf("%.0lf\n",abs(ans)); } int main() { int T; scanf("%d",&T); while(T--) { memset(d,0,sizeof(d)); memset(c,0,sizeof(c)); memset(a,0,sizeof(a)); scanf("%d%d",&n,&m); n--; for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); d[u][u]++; d[v][v]++; c[u][v]++; c[v][u]++; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=d[i][j]-c[i][j]; gauss(); } return 0; }