算法复习——矩阵树定理(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 显然不能暴力上矩阵树定理 看 ...
随机推荐
- 【转】iOS开发里的Bundle是个啥玩意?!
初学iOS开发的同学,不管是自己写的,还是粘贴的代码,或多或少都写过下面的代码 [[NSBundle mainBundle] pathForResource:@"someFileName&q ...
- 2018.5.11 Java利用反射实现对象克隆
package com.lanqiao.demo; /** * 创建人 * @author qichunlin * */ public class Person { private int id; p ...
- python plt 保存jpg出错
pip install pillow就可以解决
- c#和Java中的多态
多态:让一个对象表现出多种类型,写出通用的代码,最大限度的屏蔽各个子类之间的差异性. c#举例: 将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍. //真的鸭子 ...
- 掉坑日志:Windows Native API与DPI缩放
高DPI显示器越来越普及,软件自然也要适应这个变化,最近实习的时候也遇到了一个关于DPI缩放的问题.因为内部框架的一个控件有BUG,会导致内容的显示出问题,后来实在没办法改成了用Windows Nat ...
- NOIP2016——大家一起实现の物语
由于最近硬盘挂了,换了个固态硬盘,比赛结束后四天一直在装Linux,所以最近一直没怎么更新 看起来挺漂亮的 比赛前一个月申请停了一个月晚自习,在我们这座城市里能做到这种事情已经可以被称为奇迹了,并且在 ...
- 解决iPhone滑动不流畅问题
前段时间在做一个手机端的页面时遇到了iOS上滑动不流畅的问题,后来才发现安卓上没有问题,才意识到这是兼容性问题引起的,所以遇到问题后快速定位到问题根源非常重要.在网上一搜就找到了解决方案.以后遇到类似 ...
- jquery.imgpreload.min.js插件实现页面图片预加载
页面分享地址: http://wenku.baidu.com/link?url=_-G8miwbgDmEj6miyFtjit1duJggBCJmFjR2jky_G1VftD9eS9kwGOlFWAOR ...
- DAOMYSQLI工具类
<?php //DAOMySQLI.class.php //完成对mysql数据库操作,单例模式 //开发类 //1. 定类名 //2. 定成员属性 //3. 定成员方法[查询,dml操作] f ...
- python中打印金字塔和九九乘法表的几种方法
# 打印九九乘法表for i in range(1,10): for j in range(1,i+1): # x=i*j # print(i,'*',j,'=',x,end=' ') print(' ...