题目:

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

题解

矩阵树定理的模板题

关于矩阵数定理的证明估计在我碰线性代数前是不会了解的··而且证明太麻烦了估计学了线性代数也不会去学证明2333

但是结论很好背啊····

对于求解无向图的生成树方案树问题,我们构造一个矩阵,对角线map[i,i]为点i的度数,如果i,j连边的话map[i,j]和map[j,i]都减1(考虑到重边的情况,没有重边直接就是-1),然后去掉矩阵   最后一行最后一列,将矩阵剩余部分进行高斯消元,最后对角线乘积的绝对值就是答案了····

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=;
int T,n,m;
double map[N][N],ans;
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f;
}
inline void solve()
{
for(int i=;i<=n;i++)
{
bool flag=false;
if(!map[i][i])
{
for(int j=i+;j<=n;j++)
if(map[j][i])
{
flag=true;
for(int k=i;k<=n;k++)
swap(map[i][k],map[j][k]);
}
if(!flag) {ans=;return;}
}
for(int j=i+;j<=n;j++)
{
double temp=map[j][i]/map[i][i];
for(int k=i;k<=n;k++)
map[j][k]-=temp*map[i][k];
}
}
for(int i=;i<=n;i++)
ans*=map[i][i];
ans=(ans<?-ans:ans);
}
int main()
{
//freopen("a.in","r",stdin);
T=R();
while(T--)
{
memset(map,,sizeof(map));ans=1.0;
n=R(),m=R();int a,b;
if(n==)
{
cout<<""<<endl;
continue;
}
n--;
for(int i=;i<=m;i++)
{
a=R(),b=R();
map[a][a]++,map[b][b]++;
map[a][b]--,map[b][a]--;
}
solve();
printf("%.0lf\n",ans);
scanf("\n");
}
return ;
}

												

算法复习——矩阵树定理(spoj104)的更多相关文章

  1. SPOJ104 Highways 【矩阵树定理】

    SPOJ104 Highways Description In some countries building highways takes a lot of time- Maybe that's b ...

  2. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  3. 【算法】Matrix - Tree 矩阵树定理 & 题目总结

    最近集中学习了一下矩阵树定理,自己其实还是没有太明白原理(证明)类的东西,但想在这里总结一下应用中的一些细节,矩阵树定理的一些引申等等. 首先,矩阵树定理用于求解一个图上的生成树个数.实现方式是:\( ...

  4. spoj104 highways 生成树计数(矩阵树定理)

    https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...

  5. spoj104 HIGH - Highways 矩阵树定理

    欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...

  6. 【Learning】矩阵树定理 Matrix-Tree

    矩阵树定理 Matrix Tree ​ 矩阵树定理主要用于图的生成树计数. 看到给出图求生成树的这类问题就大概要往这方面想了. 算法会根据图构造出一个特殊的基尔霍夫矩阵\(A\),接着根据矩阵树定理, ...

  7. [洛谷U22156]未曾届到游览(矩阵树定理)

    题目背景 又到了某任*堂开关中学一年一度的自主招生考试的时间了,在考试完后许多家长决定带着自己的孩子参观一下这所距千年名校还有890周年的百年学校: 题目描述 这所学校的布局非常奇怪,是一个由N 个点 ...

  8. bzoj1016 [JSOI2008]最小生成树计数——Kruskal+矩阵树定理

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1016 从 Kruskal 算法的过程来考虑产生多种方案的原因,就是边权相同的边有一样的功能, ...

  9. BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]

    传送门 题意: 给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图$K_{n,m}$ 求生成树个数 1 <= n,m,p <= 10^18 显然不能暴力上矩阵树定理 看 ...

随机推荐

  1. UVA 1616 Caravan Robbers 商队抢劫者(二分)

    x越大越难满足条件,二分,每次贪心的选区间判断是否合法.此题精度要求很高需要用long double,结果要输出分数,那么就枚举一下分母,然后求出分子,在判断一下和原来的数的误差. #include& ...

  2. [学习笔记] SSD代码笔记 + EifficientNet backbone 练习

    SSD代码笔记 + EifficientNet backbone 练习 ssd代码完全ok了,然后用最近性能和速度都非常牛的Eifficient Net做backbone设计了自己的TinySSD网络 ...

  3. javaweb基础(2)_tomcat服务器配置

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  4. RabbitMQ Server的安装、配置及常用命令

    首先需要安装Erlang环境: http://www.rabbitmq.com/server.html 下载RabbitMQ Server的windows安装包并安装到D盘下: http://www. ...

  5. AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?

    问题 An error occurred while starting the application. ArgumentException: AddDbContext was called with ...

  6. 廖老师JavaScript教程高阶函数-sort用法

    先来学习一个新词:高阶函数 高阶函数英文叫Higher-order function.那么什么是高阶函数? JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那 ...

  7. 解决windows系统下打开应用弹出丢失libmysql.dll的问题

    只要把下载libmysql.dll,放到exe应用程序的所在目录,就可以运行,libmysql.dll有32位和64位版本,可以分别测试一下行不行,如果不行在换一个 版本试试.libmysql.dll ...

  8. 在VMware上安装centos7

    1. 下载centos7 64位镜像 linux官网下载:https://www.centos.org/download/ 2. 在VMware上安装centos7 2.1 新建虚拟机 打开虚拟机主页 ...

  9. 小程序电脑调试没有问题,真机预览报错fail hand shake error

    今天在做小程序的过程中使用HTTPS请求数据时,遇到安卓机型无法获取到数据,通过一系列的排查,发现是因为ssl证书的问题,后来通过https://www.myssl.cn/tools/check-se ...

  10. solr7.7.1完整教程

    安装 上传solr-7.7.1.tgz至服务器 opt文件加下 解压 tar -zxvf solr-7.7.1.tgz 运行 进入到加压后的文件夹/opt/solr-7.7.1,执行一下命令启动sol ...