spoj high

时间:2022-05-08 07:57:57

matrixtree定理裸体,学了行列式的n^3解法,(应该是能应用于所有行列式);

代码是参考某篇题解的。。。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int ss,x,y,T,n,m,du[];
ll g[][];
double ans,c[][],d[][];
bool zero(double x){return (x>?x:-x)<1e-;}
double guass(){
int i,j,k;
ss=;ans=;
for(i=;i<n;++i)
for(j=;j<n;++j)d[i][j]=c[i][j];
for(i=;i<n;++i){
if(zero(d[i][i])){
for(j=i+;j<n;++j)
if(!zero(d[j][i]))break;
if(j==n)return ;
for(k=i;k<n;++k)swap(d[i][k],d[j][k]);
ss++;
}
ans*=d[i][i];
for(int k=i+;k<n;++k)d[i][k]/=d[i][i];
for(int j=i+;j<n;++j)
for(int k=i+;k<n;++k)
d[j][k]-=d[j][i]*d[i][k];
}
return abs(ans);
}
int main(){
cin>>T;
while(T--){
memset(g,,sizeof(g));
memset(c,,sizeof(c));
memset(du,,sizeof(du));
cin>>n>>m;
for(int i=;i<=m;++i){
scanf("%d%d",&x,&y);
g[x][y]=g[y][x]=;
du[x]++;du[y]++;
}
for(int i=;i<=n;++i)c[i][i]=du[i];
for(int i=;i<n;++i)
for(int j=;j<n;++j){
c[i][j]=c[i][j]-g[i][j];
}
printf("%0.0lf\n",guass());
}
system("pause");
return ;
}