[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
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:
Sample output:
Solution
学了一下矩阵树定理,用来求解生成树问题。
矩阵一树定理(matrix-tree theorem)一个计数定理.若连通图G的邻接矩阵为A,将一A的对角线(i,i)元素依次换为节点V的度d(V,),所得矩阵记为M,则M的每个代数余子式相等,且等于G的支撑树的数目.这就是矩阵一树定理.
处理行列式的话,考虑高斯消元,用初等变换把M矩阵处理为上三角矩阵X,那么X的行列式就是对角线系数之积
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#define eps 1e-20
using namespace std;
int n,m,A[][],B[][];
double a[][];
void gauss(){
int now=;
for(int i=;i<=n;i++){
int x=now;
while(fabs(a[x][now])<eps && x<=n)++x;
if(x==n+){puts("");return;}
for(int j=;j<=n;j++)
swap(a[now][j],a[x][j]);
for(int j=now+;j<=n;j++){
double x=a[j][now]/a[now][now];
for(int k=;k<=n;k++)
a[j][k]-=a[now][k]*x;
}
++now;
}
double ans=;
for(int i=;i<=n;i++)
ans*=a[i][i];
printf("%.lf\n",fabs(ans));
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(A,,sizeof(A));
memset(B,,sizeof(B));
scanf("%d%d",&n,&m);
n--;
while(m--){
int u,v;
scanf("%d%d",&u,&v);
u--;v--;
A[u][u]++;A[v][v]++;
B[u][v]++;B[v][u]++;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
a[i][j]=A[i][j]-B[i][j];
gauss();
}
return ;
}
[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)的更多相关文章
- spoj104 highways 生成树计数(矩阵树定理)
https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...
- BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】
题目链接 BZOJ4031 题解 第一眼:这不裸的矩阵树定理么 第二眼:这个模\(10^9\)是什么鬼嘛QAQ 想尝试递归求行列式,发现这是\(O(n!)\)的.. 想上高斯消元,却又处理不了逆元这个 ...
- P3317 [SDOI2014]重建 变元矩阵树定理 高斯消元
传送门:https://www.luogu.org/problemnew/show/P3317 这道题的推导公式还是比较好理解的,但是由于这个矩阵是小数的,要注意高斯消元方法的使用: #include ...
- CF917D-Stranger Trees【矩阵树定理,高斯消元】
正题 题目链接:https://www.luogu.com.cn/problem/CF917D 题目大意 给出\(n\)个点的一棵树,对于每个\(k\)求有多少个\(n\)个点的树满足与给出的树恰好有 ...
- 洛谷4208 JSOI2008最小生成树计数(矩阵树定理+高斯消元)
qwq 这个题目真的是很好的一个题啊 qwq 其实一开始想这个题,肯定是无从下手. 首先,我们会发现,对于无向图的一个最小生成树来说,只有当存在一些边与内部的某些边权值相同的时候且能等效替代的时候,才 ...
- Wannafly Camp 2020 Day 1D 生成树 - 矩阵树定理,高斯消元
给出两幅 \(n(\leq 400)\) 个点的无向图 \(G_1 ,G_2\),对于 \(G_1\) 的每一颗生成树,它的权值定义为有多少条边在 \(G_2\) 中出现.求 \(G_1\) 所有生成 ...
- SP104 Highways (矩阵树,高斯消元)
矩阵树定理裸题 //#include <iostream> #include <cstdio> #include <cstring> #include <al ...
- 【BZOJ3534】【Luogu P3317】 [SDOI2014]重建 变元矩阵树,高斯消元
题解看这里,主要想说一下以前没见过的变元矩阵树还有前几个题见到的几个小细节. 邻接矩阵是可以带权值的.求所有生成树边权和的时候我们有一个基尔霍夫矩阵,是度数矩阵减去邻接矩阵.而所谓变元矩阵树实际上就是 ...
- uva10766生成树计数(矩阵树定理)
更正了我之前打错的地方,有边的话G[i][j]=-1; WA了好多次,中间要转成long double才行..这个晚点更新. #include<cstdio> #include<cs ...
随机推荐
- MySQL锁等待分析【1】
场景: 昨天业务系统上遇到了数据库慢的问题(对dcsdba.og_file_audit表的insert 慢&超时).分析后定位到是由于锁等待造成的.分析过程如下: 1.执行show proce ...
- 为什么ELF文件的加载地址是0x8048000
在一个进程的虚拟地址空间中,ELF文件是从0x8048000这个地址开始加载的,为什么会是这个地址? 回答:用命令ld --verbose可以看到0x08048000,ld的默认脚本用这个地址作为EL ...
- ImageNet && 医学图像的识别
医学图像识别的问题 如果将CNN应用于医学图像,首要面对的问题是训练数据的缺乏.因为CNN的训练数据都需要有类别标号,这通常需要专家来手工标记.要是标记像ImageNet这样大规模的上百万张的训练图像 ...
- hdu4405:概率dp
题意: 总共有n+1个格子:0-n 初始情况下在 0号格子 每次通过掷骰子确定前进的格子数 此外 还有一些传送门可以瞬间从 u 点传送到 v 点(必须被传送) 求走到(或超过)n点总共需要掷多少次骰子 ...
- java反编译工具(XJad)
java反编译工具(XJad) 2.2 绿色版 http://www.cr173.com/soft/35032.html Demo.class ---> Demo.java
- Windows系统基本概念
windows API:被文档化的可以调用的子例程,如CreateProcess 原生的系统服务(执行体系统服务):未被文档化的.可以再用户模式下调用的底层服务,如NtCreateProcess 内核 ...
- hdu 4666 Hyperspace
曼哈顿距离,两个点设为(x1,y1),(x2,y2),其距离为|x1-x2|+|y1-y2| #include <cstdio> #include <set> #include ...
- 消息摘要算法-HMAC算法
一.简述 mac(Message Authentication Code.消息认证码算法)是含有密钥散列函数算法.兼容了MD和SHA算法的特性,并在此基础上加上了密钥.因此MAC算法也常常被称作HMA ...
- Hacker(19)----检测Windows系统漏洞
想完全掌握Windows中存在的漏洞需要使用专业的漏洞扫描软件.目前常用的有MBSA(MircosoftBaselineSecurityAnalyzer).360安全卫士等. 一.使用MBSA检测系统 ...
- html游戏引擎,createJs框架
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! createJs网上的中文教程挺少的,以前UC有个Xcanvas的论坛有createJs的详细教程,但是随着XCanvas团队的解散,那个 ...