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.
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.
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.
Sample input:44 53 44 22 31 21 32 12 11 03 31 22 33 1Sample output:8113~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~行列式生成樹計數~神奇的算法~快速求出生成樹個數~對于每個點,a[i][i]為i點的度數,如果i與j之間有邊相連,則a[i][j]=-1,然后行列式求n-1矩陣的值即可~輸出一定要用%0.0lf。#include<cstdio>#include<cstring>#include<iostream>using namespace std;#define zero(u) (u>0 ? u:-u)<1e-15int t,n,m,x,y;double a[13][13],c[13][13];bool b[13][13];double cal(){ int sign=0,i,j,k;double ans=1; for(i=1;i<=n;i++) for(j=1;j<=n;j++) c[i][j]=a[i][j]; for(i=1;i<=n;i++) { if(zero(c[i][i])) { for(j=i+1;j<=n;j++) if(!zero(c[j][i])) break; if(j==n+1) return 0; for(k=i;k<=n;k++) swap(c[i][k],c[j][k]); sign++; } ans*=c[i][i]; for(k=i+1;k<=n;k++) c[i][k]/=c[i][i]; for(j=i+1;j<=n;j++) for(k=i+1;k<=n;k++) c[j][k]-=c[j][i]*c[i][k]; } if(sign&1) ans=-ans; return ans;}int main(){ scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); while(m--) { scanf("%d%d",&x,&y);b[x][y]=b[y][x]=1; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(b[i][j]) a[i][i]+=1; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(b[i][j]) a[i][j]=-1; n--;PRintf("%0.0lf/n",cal()); } return 0;}
新聞熱點
疑難解答