SPOJ Highways [矩阵树定理]
裸题
注意:
1.消元时判断系数为0,退出
2.最后乘ans要用double....
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=15; double eps=1e-8; inline int read(){ char c=getchar();int x=0; while(c<'0'||c>'9'){c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x; } int n,m,u,v; double a[N][N]; void Gauss(){ n--; for(int i=1;i<=n;i++){ int r=i; for(int j=i+1;j<=n;j++) if(abs(a[j][i])>abs(a[r][i])) r=j; if(abs(a[r][i])<eps){puts("0");return;} if(r!=i) for(int k=1;k<=n;k++) swap(a[r][k],a[i][k]); for(int j=i+1;j<=n;j++){ double t=a[j][i]/a[i][i]; for(int k=i;k<=n;k++) a[j][k]-=t*a[i][k]; } } double ans=1; for(int i=1;i<=n;i++) ans*=a[i][i]; printf("%.0f\n",abs(ans)); } int main(){ freopen("in","r",stdin); int T=read(); while(T--){ n=read();m=read(); memset(a,0,sizeof(a)); for(int i=1;i<=m;i++){ u=read();v=read(); a[u][u]++;a[v][v]++; a[u][v]--;a[v][u]--; } Gauss(); } }
Copyright:http://www.cnblogs.com/candy99/