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的更多相关文章

  1. SPOJ 104 HIGH - Highways 生成树计数

    题目链接:https://vjudge.net/problem/SPOJ-HIGH 解法: 生成树计数 1.构造 基尔霍夫矩阵(又叫拉普拉斯矩阵) n阶矩阵 若u.v之间有边相连 C[u][v]=C[ ...

  2. spoj 104 Highways(Matrix-tree定理)

    spoj 104 Highways 生成树计数,matrix-tree定理的应用. Matrix-tree定理: D为无向图G的度数矩阵(D[i][i]是i的度数,其他的为0),A为G的邻接矩阵(若u ...

  3. spoj 104 Highways (最小生成树计数)

    题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...

  4. 【SPOJ 104】HIGH - Highways (高斯消元)

    题目描述 In some countries building highways takes a lot of time- Maybe that's because there are many po ...

  5. SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)

    题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...

  6. SPOJ - HIGH :Highways (生成树计数)

    Highways 题目链接:https://vjudge.net/problem/SPOJ-HIGH Description: In some countries building highways ...

  7. 生成树计数模板 spoj 104 (不用逆元的模板)

    /* 这种题,没理解,只是记一记如何做而已: 生成树的计数--Matrix-Tree定理 题目:SPOJ104(Highways) 题目大意: *一个有n座城市的组成国家,城市1至n编号,其中一些城市 ...

  8. 基尔霍夫矩阵题目泛做(AD第二轮)

    题目1: SPOJ 2832 题目大意: 求一个矩阵行列式模一个数P后的值.p不一定是质数. 算法讨论: 因为有除法而且p不一定是质数,不一定有逆元,所以我们用辗转相除法. #include < ...

  9. 生成树的计数 Matrix-Tree(矩阵树)定理

    信息学竞赛中,有关生成树的最优化问题如最小生成树等是我们经常遇到的,而对生成树的计数及其相关问题则少有涉及.事实上,生成树的计数是十分有意义的,在许多方面都有着广泛的应用.本文从一道信息学竞赛中出现的 ...

随机推荐

  1. 《梦断代码Dreaming In Code》阅读计划

    书籍是人类宝贵的精神财富,读书是人们重要的学习方式,是人生奋斗的航灯,是文化传承的通道,是人类进步的阶梯.学生作为学习人群的主体,必须把读书作为头等大事.学校就是一个学生在教师指导下自主读书的空间,而 ...

  2. python学习笔记06:操作文件

    调用内置的open函数打开文件,传递两个参数:文件路径(绝对路径或相对路径),打开模式('r':读,'r+':读写,'w':写,'b':二进制): f = open('data.txt','w') f ...

  3. lintcode-144-交错正负数

    144-交错正负数 给出一个含有正整数和负整数的数组,重新排列成一个正负数交错的数组. 注意事项 不需要保持正整数或者负整数原来的顺序. 样例 给出数组[-1, -2, -3, 4, 5, 6],重新 ...

  4. SPDY以及HTTP2.0

    背景介绍 HTTP2.0跟SPDY在不少理念上是相似的,目的都是为了提升HTTP1.1的性能. HTTP2.0将会是业界的标准,比SPDY要完善,今后可能会都转向http2.0而放弃SPDY. SPD ...

  5. Linux下安装MySQL管理工具MySQL Administrator和MySQL Query Browser(转载)

    文章来源:http://blog.csdn.net/sunrier/article/details/7572299 Linux下MySQL Administrator和MySQL Query Brow ...

  6. java 基础--继承--007

    1,子类只能继承父类所有非私有成员 2,子类不能继承父类的构造方法,但可以通过super去访问父类构造方法 3,子类成员变量和父类成员变量名称不一样,如果一样类似于重写,按子类处理,如果一样,就近原则 ...

  7. 【bzoj2435】[NOI2011]道路修建 树形dp

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...

  8. 参考《Java疯狂讲义》

    参考<Java疯狂讲义>Lambda表达式支持将代码块作为方法参数,Lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口(这种接口被称为函数式接口)的实例 1. Lambda ...

  9. Android中WebView的跨域漏洞分析和应用被克隆问题情景还原(免Root获取应用沙盒数据)

    一.前言 去年年底支付宝的被克隆漏洞被爆出,无独有偶就是腾讯干的,其实真正了解这个事件之后会发现,感觉是针对支付宝.因为这个漏洞找出肯定花费了很大劲,主要是因为支付宝的特殊业务需要开启了WebView ...

  10. BZOJ4571:[SCOI2016]美味——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4571 https://www.luogu.org/problemnew/show/P3293 一家 ...