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. 20届的阿里 头条 网易 滴滴 百度 小米等Java面经

    转载连接 个人博客:junxuelian.cn 总结:个人感觉回答面试官问题不必太官方和书面化,腾讯sng招实习被发现照着百度百科念.结果可想而知.用自己的话和理解去回答就好.可能应届生会抱怨设计题, ...

  2. Linux error:No space left on device

    一台Oracle数据库服务器在关机重启后,Oracle监听无法启动,提示错误 Linux error:no space left on device 提示可知:问题是出在磁盘空间不足 但是初步查看分区 ...

  3. 【转】再谈PHP、Python与Ruby

    原文链接:http://www.nowamagic.net/librarys/veda/detail/2504 一句话总结 简单地总结: 假如你想帮他尽快找个活儿,赚到钱,推荐PHP. 假如你想让他成 ...

  4. Thunder团队第三周 - Scrum会议6

    Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:宋雨 工作照片: 代秋彤照相,所以图片中没有该同学. 参会成员: 王航:http://www.cnblogs ...

  5. c# 委托初窥

    1.委托可以把方法当作参数在另一个方法中传递和调用 ,委托是方法的快捷方式. 2.委托是一个类. private void BeginSocketThread() { try { IPEndPoint ...

  6. Android - 时间 日期相关组件

    源码下载地址 : -- CSDN :  http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...

  7. Alpha 冲刺(2/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前端界面的开发 搭建测试用服务器的环境 完成 ...

  8. iOS-AFNetworking与ASIHTTPRequest的区别

    一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession  2.ASI的底层实现基于纯C语言的CFNetwork框架  3.因为NSURLConnectio ...

  9. 【week4】技术随笔psp

    本周psp

  10. python爬虫从入门到放弃(四)之 Requests库的基本使用(转)

    什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...