spoj104 Highways(生成树计数模板)
来源:互联网 发布:学编程能干什么 编辑:程序博客网 时间:2024/06/06 01:32
SPOJ Problem Set (classical)
104. Highways
Problem code: HIGH
In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be connected directly. Your task is to count how many ways there are to build such a network that between every two cities there exists exactly one path. Two networks differ if there are two cities that are connected directly in the first case and aren't in the second case. At most one highway connects two cities. No highway connects a city to itself. Highways are two-way.
Input
The input begins with the integer t, the number of test cases (equal to about 1000). Then t test cases follow. The first line of each test case contains two integers, the number of cities (1<=n<=12) and the number of direct connections between them. Each next line contains two integers a and b, which are numbers of cities that can be connected. Cities are numbered from 1 to n. Consecutive test cases are separated with one blank line.
Output
The number of ways to build the network, for every test case in a separate line. Assume that when there is only one city, the answer should be 1. The answer will fit in a signed 64-bit integer.
Example
Sample input:44 53 44 22 31 21 32 12 11 03 31 22 33 1Sample output:8113
Added by:Piotr ŁowiecDate:2004-07-02Time limit:7sSource limit:50000BMemory limit:1536MBCluster:Cube (Intel Pentium G860 3GHz)Languages:All except: SCM chicken
/*提前学线代了。。。。真蛋疼。。。Time:2015-1-27 17:45*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAX=20;bool zero(double x){ return (x>0?x:-x)<1e-15;//如果x为0,返回true,否则返回false}double deg[MAX];double g[MAX][MAX];double b[MAX][MAX];double Det(int n){ int i,j,k; int sign=0; double ret=1; for(i=1;i<=n;i++){ if(zero(b[i][i])){ j=i+1; while(zero(b[j][i])&&j<=n){//找不为0的一行替换 j++; } if(j>n)return 0;//如果大于n,说明对角线一定有一个0, for(k=i;k<=n;k++){ swap(b[i][k],b[j][k]); } sign++; } if(zero(b[i][i])) return 0; ret*=b[i][i]; for(j=i+1;j<=n;j++){ double tmp=b[j][i]/b[i][i]; for(k=i+1;k<=n;k++){ b[j][k]-=tmp*b[i][k]; } } } if(sign&1) ret=-ret; return ret;}int main(){ int T,n,m; int u,v; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); memset(deg,0,sizeof(deg)); memset(g,0,sizeof(g)); memset(b,0,sizeof(b)); while(m--){ scanf("%d%d",&u,&v); g[u][v]=1;g[v][u]=1; deg[u]++; deg[v]++; } for(int i=1;i<=n;i++){ b[i][i]=deg[i]; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ b[i][j]-=g[i][j]; } } /*for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ printf(" %.0lf",b[i][j]); }puts(""); }*/ printf("%.0lf\n",Det(n-1));//传入的是n-1,任意n-1阶就行 }return 0;}
- spoj104 Highways(生成树计数模板)
- SPOJ104 Highways,生成树计数
- SPOJ104(Highways) 生成树计数 Matrix-Tree定理
- 生成树计数总结 SPOJ104
- spoj104 highways(矩阵树定理)
- [SPOJ104]Highways(矩阵树定理)
- SPOJ104:Highways(矩阵树定理)
- 【SPOJ104】【矩阵树定理】Highways
- SPOJ Highways(生成树计数)
- SPOJ HighWays 生成树计数 模板题(Matrix-Treed定理)
- 【SPOJ104】Highways
- SPOJ104. Highways
- 【spoj104】highways
- SPOJ 104 HIGH - Highways(生成树计数 不取模)
- 生成树计数 spoj 104 highways
- SPOJ 104 Highways 最小生成树计数
- SPOJ 104 Highways(生成树计数)
- SPOJ HIGH Highways(生成树计数,Matrix-Tree定理)
- NIT ACM 2014 寒假集训#1 初阶组
- Thinkphp验证码出现乱码处理方案
- Ubuntu下终端路径定制处理
- 客户端套接字(Socket)的超时
- JS中随机颜色的获取
- spoj104 Highways(生成树计数模板)
- 正则表达式语法工具
- Socket类的getter和setter方法(1)
- UVA 11971 Polygon
- Java架构师与开发者提高效率的10个工具
- 等分
- oracle的内建函数
- Linux grep匹配特殊字符
- DOM扩展——笔记