算法复习——矩阵树定理(spoj104)
题目:
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)的更多相关文章
- SPOJ104 Highways 【矩阵树定理】
SPOJ104 Highways Description In some countries building highways takes a lot of time- Maybe that's b ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- 【算法】Matrix - Tree 矩阵树定理 & 题目总结
最近集中学习了一下矩阵树定理,自己其实还是没有太明白原理(证明)类的东西,但想在这里总结一下应用中的一些细节,矩阵树定理的一些引申等等. 首先,矩阵树定理用于求解一个图上的生成树个数.实现方式是:\( ...
- spoj104 highways 生成树计数(矩阵树定理)
https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...
- spoj104 HIGH - Highways 矩阵树定理
欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...
- 【Learning】矩阵树定理 Matrix-Tree
矩阵树定理 Matrix Tree 矩阵树定理主要用于图的生成树计数. 看到给出图求生成树的这类问题就大概要往这方面想了. 算法会根据图构造出一个特殊的基尔霍夫矩阵\(A\),接着根据矩阵树定理, ...
- [洛谷U22156]未曾届到游览(矩阵树定理)
题目背景 又到了某任*堂开关中学一年一度的自主招生考试的时间了,在考试完后许多家长决定带着自己的孩子参观一下这所距千年名校还有890周年的百年学校: 题目描述 这所学校的布局非常奇怪,是一个由N 个点 ...
- bzoj1016 [JSOI2008]最小生成树计数——Kruskal+矩阵树定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1016 从 Kruskal 算法的过程来考虑产生多种方案的原因,就是边权相同的边有一样的功能, ...
- BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]
传送门 题意: 给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图$K_{n,m}$ 求生成树个数 1 <= n,m,p <= 10^18 显然不能暴力上矩阵树定理 看 ...
随机推荐
- C++内存溢出和内存泄漏?
1.内存溢出 内存溢出是指程序在申请内存时没有足够的内存空间供其使用.原因可能如下: (1)内存中加载的数据过于庞大: (2)代码中存在死循环: (3)递归调用太深,导致堆栈溢出等: (4)内存泄漏最 ...
- python学习笔记-环境安装【1】
1.在 WINDOWS 下面要运行命令 pip install virtualenvwrapper-win才行 参考地址http://blog.csdn.net/liuhongyue/article/ ...
- Hibernate中session的save方法问题
今天在使用session.save(),进行插入数据操作时,一直没有成功,也没有报错.后来发现是因为没有创建事务,提交事务的原因 你对flush和commit的意思没有理解到:1,flush代表刷新, ...
- iOS 开发 Xib 的嵌套使用
最近公司项目需要使用 Xib 中嵌套 Xib来布局界面的, 研究了很久才实现!!! 分享给大家,希望帮助到更多的开发者...... 开发中自定义界面有两种方式 一: 纯代码实现 适合单个极度复杂的界面 ...
- Chunky Monkey-freecodecamp算法题目
Chunky Monkey(猴子吃香蕉, 分割数组) 要求 把一个数组arr按照指定的数组大小size分割成若干个数组块. 思路 利用size值和while语句确定切割数组的次数(定义temp将siz ...
- 配置httpd虚拟主机
轻松配置httpd的虚拟主机 httpd使用VirtualHost指令进行虚拟主机的定义.支持三种虚拟主机:基于ip,基于端口和基于名称.其中基于端口的虚拟主机在httpd的术语上(例如官方手册)也属 ...
- 拖拽图片到另一个div里
HTML代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- Maven配置项目依赖使用本地仓库的方法汇总
Maven配置项目使用本地仓库有以下方式实现: 1.类似本地仓库,但是属于本地依赖,比如某个JAR包是引用第三方的,直接放在了项目的lib文件夹,那么此时可以如下配置项目的POM: <depen ...
- pandas处理较大数据量级的方法 - chunk,hdf,pkl
前情提要: 工作原因需要处理一批约30G左右的CSV数据,数据量级不需要hadoop的使用,同时由于办公的本本内存较低的缘故,需要解读取数据时内存不足的原因. 操作流程: 方法与方式:首先是读取数据, ...
- Django与多个数据库交互
定义数据库 在Django中使用多个数据库的第一步是告诉Django您将要使用的数据库服务器. 数据库可以有您选择的任何别名.但是,别名 default 有着特殊的意义.Django使用别名为 def ...