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

图论 生成树 Matrix-Tree定理

按照给出的边关系建立邻接矩阵和度数矩阵,直接套用矩阵树定理。

给出的图可能不连通……无解时候要输出0

↑天真的我压根儿没想到无解的情况,WA了一串

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const double eps=1e-;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
double G[mxn][mxn],A[mxn][mxn];
int n,m;
void Solve(){
int i,j;
double ans=;
for(i=;i<n;i++){
int p=i;
if(fabs(G[i][i])<eps){
for(j=i+;j<n;j++){
if(fabs(G[j][i])>eps)p=j;
}
if(p==i){
printf("0\n");return;
}
for(j=;j<n;j++)swap(G[i][j],G[p][j]);
ans=-ans;
}
for(j=i+;j<n;j++){
double c=G[j][i]/G[i][i];
for(int k=i;k<n;k++){
G[j][k]-=c*G[i][k];
}
}
ans*=G[i][i];
}
printf("%.0f\n",ans);
return;
}
int main(){
int i,j,u,v;
int T;
scanf("%d",&T);
while(T--){
memset(G,,sizeof G);
memset(A,,sizeof A);
scanf("%d%d",&n,&m);
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
++G[u][u];
++G[v][v];
++A[u][v];++A[v][u];
}
for(i=;i<=n;i++)
for(j=;j<=n;j++){
G[i][j]-=A[i][j];
}
Solve();
}
return ;
}

SPOJ HIGH Highways的更多相关文章

  1. 【SPOJ】Highways(矩阵树定理)

    [SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...

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

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

  3. 生成树的计数(基尔霍夫矩阵):UVAoj 10766 Organising the Organisation SPOJ HIGH - Highways

    HIGH - Highways   In some countries building highways takes a lot of time... Maybe that's because th ...

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

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

  5. SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元

    [题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...

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

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

  7. SPOJ - HIGH Highways(矩阵树定理)

    https://vjudge.net/problem/SPOJ-HIGH 题意: 给n个点m条边,求生成树个数. 思路: 矩阵树裸题. 具体的话可以看一下周冬的论文<生成树的计数及其应用> ...

  8. [spoj] HIGH - Highways (生成树计数)

    传送门 输入格式: 第一行一个整数T,表示测试数据的个数 每个测试数据第一行给出 n,m 分别表示点数与边数 接下来 m 行,每行给出两个数 a,b ,表示 a,b 之间有一条无向边 输出格式: 每个 ...

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

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

随机推荐

  1. Mysql--子查询、分页查询、联合查询

    一. 子查询的定义 出现在其他语句中的select语句,称为子查询或者内查询,外部的查询语句称为主查询或者外查询,子查询可以包含普通select可以包含的任何语句. 外部查询:select.inser ...

  2. http 实战练习

    http 实战练习 建立httpd服务器,要求提供两个基于名称的虚拟主机: (1)www.X.com,页面文件目录为/web/vhosts/x:错误日志为/var/log/httpd/x.err,访问 ...

  3. nginx修改nginx.conf配置可以https访问

    修改nginx.conf,参照如下更改配置server { listen 443; server_name abc.com; // 访问域名 ssl on; root /var/www/bjubi.c ...

  4. 五 python并发编程之IO模型

    一 IO模型介绍 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问 ...

  5. manjaro中文输入法已安装但切换不了解决方法

    情况如图所示,输入法安装了,但Ctrl+空格键或者鼠标选择切换都不行 解决方法: 打开家目录下面的.xprofile文件,如果没有这个文件就新建一个,加入下面内容 保存文件,退出. 重启电脑就可以了

  6. CyclicBarrier源码分析

    CyclicBarrier是通过ReentrantLock(独占锁)和Condition来实现的.下面,我们分析CyclicBarrier中3个核心函数: 构造函数, await()作出分析. 1. ...

  7. android 极光推送 声音与振动 的关闭和开启

    前言:最近刚好在写一些推送方面的东西,又是新手,不断在网上找资料,很少,不过还是找到了一些,反正百度我是再也不想百度了,谷歌一下子就能找到想要的. 废话不多说. 1.主要方法就是如下一个函数 priv ...

  8. Python虚拟机类机制之从class对象到instance对象(五)

    从class对象到instance对象 现在,我们来看看如何通过class对象,创建instance对象 demo1.py class A(object): name = "Python&q ...

  9. 【面试】一篇文章帮你彻底搞清楚“I/O多路复用”和“异步I/O”的前世今生

    曾经的VIP服务 在网络的初期,网民很少,服务器完全无压力,那时的技术也没有现在先进,通常用一个线程来全程跟踪处理一个请求.因为这样最简单. 其实代码实现大家都知道,就是服务器上有个ServerSoc ...

  10. Django之session验证的三种姿势

    一.什么是session session是保存在服务端的键值对,Django默认支持Session,并且默认是将Session数据存储在数据库中,即:django_session 表中. 二.FVB中 ...