spoj104 highways(矩阵树定理)
来源:互联网 发布:网络招聘怎么做 编辑:程序博客网 时间:2024/05/16 12:19
HIGH - Highways
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
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;const int maxn=107;double c[maxn][maxn];int a[maxn][maxn],b[maxn][maxn];int n;double eps=1e-8;double det(){int i,j,k,r;for(i=1;i<=n;i++){r=i;for(j=i+1;j<=n;j++)if(fabs(c[j][i])>fabs(c[r][i])) r=j;if(c[r][i]<eps){return 0;}if(r!=i) for(j=1;j<=n;j++) swap(c[i][j],c[r][j]);for(j=i+1;j<=n;j++){double f=c[j][i]/c[i][i];for(k=i;k<=n;k++){c[j][k]-=f*c[i][k];}}}double ans=1.0;if(n&1) ans=ans;for(int i=1;i<=n;i++){ans*=c[i][i];}return ans;}int main(){int t;scanf("%d",&t);while(t--){int m;scanf("%d%d",&n,&m);memset(a,0,sizeof(a));memset(b,0,sizeof(b));n--;for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);u--;v--;a[u][u]++;a[v][v]++;b[u][v]++;b[v][u]++;}for(int i=1;i<=n;i++)for(int k=1;k<=n;k++)c[i][k]=(double)a[i][k]-b[i][k];printf("%.0lf",det());}return 0;}
- spoj104 highways(矩阵树定理)
- [SPOJ104]Highways(矩阵树定理)
- SPOJ104:Highways(矩阵树定理)
- 【SPOJ104】【矩阵树定理】Highways
- SPOJ104(Highways) 生成树计数 Matrix-Tree定理
- spoj104 Highways(生成树计数模板)
- SPOJ104 Highways,生成树计数
- 【SPOJ104】Highways
- SPOJ104. Highways
- 【spoj104】highways
- spoj highways 【生成树定理】
- 矩阵树定理(Matrix -Tree定理)
- 生成树计数--矩阵树定理(Matrix-Tree定理)
- 生成树计数总结 SPOJ104
- bzoj4031 (矩阵树定理,行列式基础)
- [bzoj1002]: [FJOI2007]轮状病毒(矩阵树定理)
- 矩阵树定理
- 矩阵树定理
- Struts2小结-ValueStack-OGNL-EL
- C语言的一些优缺点
- node js- 路由转发和失败重试策略
- C语言入门级教程三
- SSM搭建-Spring MVC搭建第一个HelloWorld(12)
- spoj104 highways(矩阵树定理)
- ECSHOP后台编辑器上传中文名称图片乱码解决方法
- JAVA 中 Redis与ehcache对比与使用
- python3中报错:TypeError: 'range' object doesn't support item deletion
- Java回顾之多线程同步
- jQuery Ajax 实例详解 ($.ajax、$.post、$.get)
- html5笔记之新增选择器
- APUE笔记---第六章系统数据文件和信息操作
- Qt学习之路QString