Description


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.

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

Solution


论文例题 周冬:《生成树的计数及其应用》
题意大概是给定一个没有重边和自环的图,求其中生成树的方案数
=w=矩阵树定理

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#define eps 1e-9
using namespace std;
int T,n,m;
double a[15][15];
int dcmp(double x)
{if(fabs(x)<eps)return 0;return x>0?1:-1;
}
void solve()
{int col=1;double ans=1;for(int i=1;i<=n;i++){int maxline=i;for(int j=i+1;j<=n;j++){if(a[j][col]>a[maxline][col])maxline=j;}if(!dcmp(a[maxline][col])){printf("0\n");return;}for(int j=1;j<=n;j++){swap(a[i][j],a[maxline][j]);}for(int j=i+1;j<=n;j++){double t=a[j][col]/a[i][col];for(int k=col;k<=n;k++){a[j][k]-=a[i][k]*t;}}col++;}for(int i=1;i<=n;i++)ans*=a[i][i];ans=fabs(ans);printf("%.0lf\n",ans);
}
void init()
{memset(a,0,sizeof(a));scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);a[u][u]++;a[v][v]++;a[u][v]=a[v][u]=-1;}n--;
}
int main()
{scanf("%d",&T);while(T--){init();solve();}return 0;
}

[SPOJ P104]HIGH - Highways(Matrix-Tree定理)相关推荐

  1. [学习笔记] Matrix tree定理

    前言 为了回归算法本身,为了搞懂算法,我参考了不同博客,并将他们做出整合和补充: https://www.luogu.com.cn/blog/tanrui-2960967961/matrix-tree ...

  2. spoj p104 Matrix-Tree定理

    这个问题就是经典的生成树记数问题,题目为spoj p104 highway. 首先我们引入Matrix-Tree定理,由kirchhoff证明,定理的概述为,对于图G,我们定义若干个矩阵, D[G], ...

  3. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7669  Solved: 1894 [Sub ...

  4. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  5. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  6. bzoj 1016 [JSOI2008]最小生成树计数——matrix tree(相同权值的边为阶段缩点)(码力)...

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1016 就是缩点,每次相同权值的边构成的联通块求一下matrix tree.注意gauss里的 ...

  7. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  8. 计算任意一个图生成树的个数——Kirchhoff 的Matrix Tree 方法Java实现

    计算任意一个图的生成树的个数,是Kirchhoff提出的理论,通常称为Matrix Tree Theorem,原理很简单: Let G be a graph with V(G)={v1,v2,..., ...

  9. [BZOJ4596][Shoi2016]黑暗前的幻想乡-Matrix Tree 矩阵树定理

    黑暗前的幻想乡 Description 四年一度的幻想乡大选开始了,最近幻想乡最大的问题是很多来历不明的妖怪涌入了幻想乡,扰乱了幻想乡昔日的秩序.但是幻想乡的建制派妖怪(人类)博丽灵梦和八云紫等人整日 ...

最新文章

  1. def __init__(self)是什么意思_子类必须调用 super().__init__() 吗?
  2. python requests post请求_实例解析Python3 如何利用requests 库进行post携带账号密码请求数据...
  3. elastic ik分词搜索_php环境下使用elasticSearch+ik分词器进行全文搜索
  4. C++的const比C语言#define更好的原因?
  5. nrm : 无法加载文件 C:\Users\hc\AppData\Roaming\npm\nrm.ps1 ,因为在此系统上禁止运行脚本。
  6. 如何知道对象在Python中是否具有属性
  7. Intent以及IntentFilter详解 1
  8. 实对称矩阵不同特征值对应的特征向量都正交
  9. 直流有刷电机(可以用L298N来驱动)
  10. 微信开发者工具如何使用SCSS和配置wxml文件高亮及格式化
  11. mysql 身份证号码判断
  12. 前端过滤特殊字符、表情包
  13. 在浏览器端浏览EPUB
  14. 2022-2028年中国汽车设计行业发展现状调查及市场分析预测报告
  15. 腾讯企业 html邮件模板,python连接腾讯企业邮箱发送html邮件
  16. ROS掉包侠修炼计划
  17. 遥感原理与应用总结——第七章:遥感图像判读
  18. ERP : 能力需求计划
  19. 携程加入直播行列:负债近千亿 疫情后用户面临退款难
  20. python 矩阵列拼接_numpy数组拼接简单示例

热门文章

  1. 利用短时傅里叶变换(STFT)对信号进行时频谱分析和去噪声
  2. 快手如何投放广告?费用怎么计算的?
  3. @Autowired注解详解
  4. 20189220 余超《Linux内核原理与分析》第二周作业
  5. Azure BareMetal 裸金属
  6. 【有限马尔科夫链状态分解+Kosaraju 算法】基于Kosaraju 算法和可达矩阵的有限马尔科夫链状态分解
  7. 图表背后的秘密 | 技术指标讲解:ATR指标
  8. base64编码类------原始码(C#)
  9. mysql slave_pending_jobs_size_max_MySQL:关于Wating for Slave workers to free pending events等待...
  10. 计算机网络常用相关术语大全