SPOJ 104 HIGH - Highways
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:
4
4 5
3 4
4 2
2 3
1 2
1 3 2 1
2 1 1 0 3 3
1 2
2 3
3 1 Sample output:
8
1
1
3
生成树计数
1、构造 基尔霍夫矩阵(又叫拉普拉斯矩阵)
n阶矩阵
若u、v之间有边相连 C[u][v]=C[v][u]=-1
矩阵对角线为点的度数
2、求n-1阶主子式 的行列式的绝对值
去掉第一行第一列
初等变换消成上三角矩阵
对角线乘积为行列式
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int n;
LL C[][],tmp[];
int main()
{
int T,m,u,v;
LL t,ans;
scanf("%d",&T);
while(T--)
{
memset(C,,sizeof(C));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&u,&v);
u--; v--;
C[u][v]=-; C[v][u]=-;
C[u][u]++; C[v][v]++;
}
ans=;
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
while(C[j][i])
{
t=C[i][i]/C[j][i];
for(int k=i;k<n;k++) C[i][k]-=C[j][k]*t;
for(int k=i;k<n;k++) swap(C[i][k],C[j][k]);
ans=-ans;
}
ans*=C[i][i];
if(!ans) break;
}
if(ans<) ans=-ans;
printf("%lld\n",ans);
}
}
SPOJ 104 HIGH - Highways的更多相关文章
- SPOJ 104 HIGH - Highways 生成树计数
题目链接:https://vjudge.net/problem/SPOJ-HIGH 解法: 生成树计数 1.构造 基尔霍夫矩阵(又叫拉普拉斯矩阵) n阶矩阵 若u.v之间有边相连 C[u][v]=C[ ...
- spoj 104 Highways(Matrix-tree定理)
spoj 104 Highways 生成树计数,matrix-tree定理的应用. Matrix-tree定理: D为无向图G的度数矩阵(D[i][i]是i的度数,其他的为0),A为G的邻接矩阵(若u ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
- 【SPOJ 104】HIGH - Highways (高斯消元)
题目描述 In some countries building highways takes a lot of time- Maybe that's because there are many po ...
- SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)
题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...
- SPOJ - HIGH :Highways (生成树计数)
Highways 题目链接:https://vjudge.net/problem/SPOJ-HIGH Description: In some countries building highways ...
- 生成树计数模板 spoj 104 (不用逆元的模板)
/* 这种题,没理解,只是记一记如何做而已: 生成树的计数--Matrix-Tree定理 题目:SPOJ104(Highways) 题目大意: *一个有n座城市的组成国家,城市1至n编号,其中一些城市 ...
- 基尔霍夫矩阵题目泛做(AD第二轮)
题目1: SPOJ 2832 题目大意: 求一个矩阵行列式模一个数P后的值.p不一定是质数. 算法讨论: 因为有除法而且p不一定是质数,不一定有逆元,所以我们用辗转相除法. #include < ...
- 生成树的计数 Matrix-Tree(矩阵树)定理
信息学竞赛中,有关生成树的最优化问题如最小生成树等是我们经常遇到的,而对生成树的计数及其相关问题则少有涉及.事实上,生成树的计数是十分有意义的,在许多方面都有着广泛的应用.本文从一道信息学竞赛中出现的 ...
随机推荐
- CSS动画@-webkit-keyframes
@-webkit-keyframes:以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%.0% 是动画的开始时 ...
- HADOOP docker(五):hadoop用户代理 Proxy user
1.hadoop用户代理简介2.配置3.实验 1.hadoop用户代理简介 hadoop用户代理功能的作用是让超级用户superuser模拟一个普通用户来执行任务.比如用户joe通过oozie提交一个 ...
- wpa_supplicant之eloop_run分析
部分内容转自http://blog.chinaunix.net/uid-20273473-id-3128151.html 重要结构体!!! struct eloop_sock { int sock; ...
- 衡量经济活动的价值:国内生产总值(GDP, Gross Domestic Product)
定义 GDP是在给定的时期内,经济生产的所有最终产品和服务的市场价值. 由于每一件产品或者服务的交易都会涉及到一个买者和一个卖着,买者支出的每一元钱必然成为卖者收入的每一元钱,因此,GDP既可以看成是 ...
- C++ STL victor
一.介绍 vector是表示可变大小数组的序列容器. 就像数组一样,vector也采用的连续存储空间来存储元素.也就是意味着可以采用下标对vector的元素进行访问,和数组一样高效.但是又不像数组,它 ...
- Discover the Web(栈模拟)
Description Standard web browsers contain features to move backward and forward among the pages rece ...
- 一个项目的Makefile编写及调试
父Makefile 在src目录下包含很多文件夹,那么需要遍历所有的目录执行Makefile,那么给一个在src目录下的Makefile. # 需要排除的目录 exclude_dirs := incl ...
- AngularJS 中特性(attr)和属性(prop)的区别
attr() 和 removeAttr() 方法是对特性进行处理的, 而 prop() 是对属性进行操作的 , 但是很多时候操作的东西是同一个 , 但是也是有区别的, 区别在于prop方法处理的是被 ...
- 一日一句 SQL [持续更新] MySQL + Oracle
1 . group by 和 having字句: group by是根据列值对数据进行分组, having子句用于对分组的数据进行过滤. [ having 针对的对象是分好的组] eg: employ ...
- 自定义JS Map 函数
// 自定义JS Map 函数 function Map() { var map = function (key, value) {//键值对 this.key = key; this.value = ...