矩阵树定理大概是说一张图的基尔霍夫矩阵由度数矩阵减去邻接矩阵得到,
使用这张图的边的生成树个数就等于基尔霍夫矩阵的det
存个求行列式的模板吧
//#include<bits/stdc++.h> //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<algorithm> #include<queue> #include<string.h> #include<iostream> #include<math.h> #include<set> #include<map> #include<vector> #include<iomanip> using namespace std; const double pi=acos(-1.0); #define ll long long #define pb push_back #define sqr(a) ((a)*(a)) #define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)) const double eps=1e-10; const int maxn=2e2+56; const int inf=0x3f3f3f3f; const int mod=1e9+7; int T,n,m,u,v; double du[maxn],a[maxn][maxn],ans; void clear(){ n=m=u=v=0;ans=0; memset(du,0,sizeof du);memset(a,0,sizeof a); } double gauss(){ for(int i=1;i<n;i++){ int num=i; for(int j=i+1;j<n;j++) if(fabs(a[j][i])>fabs(a[num][i]))num=j; if(num!=i) for(int j=1;j<n;j++)swap(a[i][j],a[num][j]); for(int j=i+1;j<n;j++) if(fabs(a[j][i])>eps){ double t=a[j][i]/a[i][i]; for(int k=1;k<n;k++)a[j][k]-=a[i][k]*t; } } double ans=1.0; for(int i=1;i<n;i++)ans*=a[i][i]; return (ans>0)?ans:-ans; } int main(){ scanf("%d",&T); while(T--){ clear(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); ++du[u];++du[v]; if(u!=v)a[u][v]=a[v][u]=-1.0; } for(int i=1;i<=n;i++)a[i][i]=du[i]+0.0; ans=gauss(); printf("%.0lf ",ans+eps); } }