模板题。
Matrix−Tree M a t r i x − T r e e 定理用于求生成树个数。
给出一个无向图 G G , 的度数矩阵 D D 是一个 的矩阵,当 i≠j i ≠ j 时, di,j=0 d i , j = 0 , di,i d i , i 等于 i i 的度数。 的邻接矩阵为 A A 。
我们定义 的 Kirchhoff K i r c h h o f f 矩阵为 D−A D − A ,则 Matrix−Tree M a t r i x − T r e e 定理可以描述为:
G G 的所有不同的生成树的个数等于其 矩阵 C[G] C [ G ] 任何一个主对角线上的余子式 Mi,i M i , i 的绝对值。
#include<cstdio>
#include<cctype>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=55;
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int getint(){
char ch=gc(); int res=0,ff=1;
while(!isdigit(ch)) ch=='-'?ff=-1:0, ch=gc();
while(isdigit(ch)) res=(res<<3)+(res<<1)+ch-'0', ch=gc();
return res*ff;
}
int n,m,Q;
double A[maxn][maxn];
void Guass(){
for(int i=1;i<=n-1;i++){
int pos=0; double _max=0;
for(int j=i;j<=n;j++) if(fabs(A[j][i])>_max) _max=fabs(A[j][i]), pos=j;
if(!pos) continue;
for(int j=1;j<=n;j++) swap(A[i][j],A[pos][j]);
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]-=t*A[i][k];
}
}
}
inline LL getDet(){
double res=1;
for(int i=1;i<=n;i++) res*=A[i][i];
return res+0.1;
}
int main(){
freopen("spojHIGH.in","r",stdin);
freopen("spojHIGH.out","w",stdout);
Q=getint();
while(Q--){
n=getint(); m=getint();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) A[i][j]=0;
for(int i=1;i<=m;i++){
int x=getint(),y=getint();
A[x][x]++; A[y][y]++; A[x][y]--; A[y][x]--;
}
n--; Guass();
printf("%lld\n",abs(getDet()));
}
return 0;
}