题目:

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. UVA439 knightMoves (A*启发搜索)

    第一个A*,纪念下. A*要保证最短路一定要估价函数小于等于实际值,越接近越好 估价函数取Manhattan距离除以二. //Rey #include<cstdio> #include&l ...

  2. xcdatamodel的实质

    修改后缀名为zip或者其它,可以查看到xcdatamodel是一个描述文件 <?xml version="1.0" encoding="UTF-8" st ...

  3. 解决TS报错Property 'style' does not exist on type 'Element'

    在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...

  4. mysql group by的特殊性

    SELECT create_year, userno , sum(sal) FROM user GROUP BY userno 以上语句,在oracle 或sql server肯定是语法错误  因为g ...

  5. Bootstrap历练实例:带表格的面板

    带表格的面板 为了在面板中创建一个无边框的表格,我们可以在面板中使用 class .table.假设有个 <div> 包含 .panel-body,我们可以向表格的顶部添加额外的边框用来分 ...

  6. 功能强大的CURL

      linux下的curl,有着非同一般的魔力,有人称它为下载工具,我更倾向于叫它“文件传输工具”因为它好像无所不能.从常见的 FTP, HTTP, TELNET, 等协议,还支持代理服务器,cook ...

  7. 基于matlab的蓝色车牌定位与识别---识别

    接着昨天的工作,把最后一部分识别讲完. 关于字符识别这块,一种最省事的办法是匹配识别,将所得的字符和自己的标准字符库相减,计算所得结果,值最小的即为识别的结果.不过这种方法是在所得字符较为标准的情况, ...

  8. 不使用脚手架的 vue 应用

    工作中的项目不止有页面繁多的模块化项目,还会只有一两个页面的类似于填写信息参与活动的活动页.这个时候,就可以回归以前的三剑客模式,在 index.html 里引用 vue.js 进行开发. 关键点: ...

  9. JQuery 在线编辑器和手册

    JQuery 在线编辑器 JQuery 在线编辑器 JQuery 菜鸟教程 手册 JQuery 菜鸟教程 手册

  10. python日记整理

    都是自己的学习总结,要是总结的有问题大佬麻烦评价一下我好修改,谢谢 python插件插件+pycharm基本用法+markdown文本编写+jupyter notebook的基本操作汇总 一.计算机基 ...