题目:

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. python 与 json

    +-------------------+---------------+    | Python            | JSON          |    +================= ...

  2. apropos linux

    Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...

  3. java基础—static关键字

    一.static关键字

  4. cocos2dx lua 吞噬层的触摸事件

    首先要创建一个layer,设置该层为可触摸 layer:setTouchEnabled(true) 注册触摸事件 local listener = cc.EventListenerTouchOneBy ...

  5. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  6. 性能优化之MySQL优化(慕课)

    MySQL数据库优化 1-1MySQL优化简介 数据库优化的目的 避免出现页面访问错误 由于数据库连接timeout产生5XX错误 由于慢查询造成页面无法加载 由于阻塞造成数据无法提交 增加数据库的稳 ...

  7. 记一次header跨域与cookie共享

       最近把左边的传统模式,换成了右边通过js直接调api拿数据并渲染,于是变出现了ajax的跨域问题:XMLHttpRequest cannot load http://api.abc.com/?s ...

  8. 《零基础入门学习Python》【第一版】视频课后答案第002讲

    测试题答案: 0. 什么是BIF?BIF 就是 Built-in Functions,内置函数.为了方便程序员快速编写脚本程序(脚本就是要编程速度快快快!!!),Python 提供了非常丰富的内置函数 ...

  9. 在ArchLinux、manjaro中安装MySql(mariaDB)

    安装MySql数据库.但是在MySql被Oracle收购之后,很多开源支持者就转而使用MariaDb了.不过MariaDb也和MySql兼容的,所以基本不用有什么担心.由于ArchLinux只带了Ma ...

  10. Oracle redo与undo 第一弹

      一. 什么是redo(用于前滚数据) redo也就是重做日志文件(redo log file),Oracle维护着两类重做日志文件:在线(online)重做日志文件和归档(archived)重做日 ...