spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/
题意:求最小生成树个数。
1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 #define ll long long 7 double a[205][205]; 8 int n,m; 9 const double eps=1e-15; 10 int zero(double x){ 11 if (x<-eps) return -1; 12 else return x>eps; 13 } 14 double work(){ 15 double res=1; 16 for (int i=1;i<=n;i++){ 17 int k=i; 18 for (int j=i+1;j<=n;j++) if (fabs(a[j][i])>fabs(a[k][i])) k=j; 19 if (k!=i){ 20 for (int j=1;j<=n;j++) 21 std::swap(a[k][j],a[i][j]); 22 } 23 for (int j=i+1;j<=n;j++){ 24 double tmp=a[j][i]/a[i][i]; 25 for (int k=i;k<=n;k++) 26 a[j][k]-=tmp*a[i][k]; 27 } 28 if (!zero(a[i][i])) return 0; 29 } 30 for (int i=1;i<=n;i++) res*=a[i][i]; 31 return std::fabs(res); 32 } 33 int main(){ 34 int T; 35 scanf("%d",&T); 36 while (T--){ 37 scanf("%d%d",&n,&m); 38 for (int i=0;i<=n;i++) 39 for (int j=0;j<=n;j++) 40 a[i][j]=0; 41 while (m--){ 42 int x,y; 43 scanf("%d%d",&x,&y); 44 a[x][x]+=1.0;a[y][y]+=1.0; 45 a[x][y]-=1.0;a[y][x]-=1.0; 46 } 47 n--; 48 printf("%0.0lf\n",work()); 49 } 50 }