博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法复习——矩阵树定理(spoj104)
阅读量:5280 次
发布时间:2019-06-14

本文共 2560 字,大约阅读时间需要 8 分钟。

题目:

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:44 53 44 22 31 21 32 12 11 03 31 22 33 1Sample output:8113

题解

  矩阵树定理的模板题

  关于矩阵数定理的证明估计在我碰线性代数前是不会了解的··而且证明太麻烦了估计学了线性代数也不会去学证明2333

  但是结论很好背啊····

  对于求解无向图的生成树方案树问题,我们构造一个矩阵,对角线map[i,i]为点i的度数,如果i,j连边的话map[i,j]和map[j,i]都减1(考虑到重边的情况,没有重边直接就是-1),然后去掉矩阵   最后一行最后一列,将矩阵剩余部分进行高斯消元,最后对角线乘积的绝对值就是答案了····

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int N=20;int T,n,m;double map[N][N],ans;inline int R(){ char c;int f=0; for(c=getchar();c<'0'||c>'9';c=getchar()); for(;c<='9'&&c>='0';c=getchar()) f=(f<<3)+(f<<1)+c-'0'; return f;}inline void solve(){ for(int i=1;i<=n;i++) { bool flag=false; if(!map[i][i]) { for(int j=i+1;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=0;return;} } for(int j=i+1;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=1;i<=n;i++) ans*=map[i][i]; ans=(ans<0?-ans:ans);}int main(){ //freopen("a.in","r",stdin); T=R(); while(T--) { memset(map,0,sizeof(map));ans=1.0; n=R(),m=R();int a,b; if(n==1) { cout<<"1"<

 

 
 

转载于:https://www.cnblogs.com/AseanA/p/7597727.html

你可能感兴趣的文章
Python--GIL 详解
查看>>
Oracle数据导入Mysql中
查看>>
BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)
查看>>
MongoDB学习笔记——聚合操作之group,distinct,count
查看>>
大道至简读后感(第四章)
查看>>
IDA IDC Tutorials: Additional Auto-Commenting
查看>>
k8s-存储卷1-十二
查看>>
Android 自定义ViewGroup实现弧形菜单
查看>>
ABP框架系列之四十一:(Nuget-Packages-Nuget包)
查看>>
head first 设计模式文摘
查看>>
在Android中Intent的概念及应用(二)——Intent过滤器相关选项
查看>>
数据库备份问题
查看>>
11.Java2核心技术—Java5.0新特性
查看>>
前端面试题(4)iframe有哪些优点?iframe缺点是什么?
查看>>
一道腾讯面试题的思考:到底谁会赢?
查看>>
【Bugly干货分享】微信文件微起底Ⅰ
查看>>
大数据 技术入门02
查看>>
参考着做个方法,处理js不能触发value改变事件,---只是定时器用时间间隔检查和处理,如果要求严格的,可能并不适用...
查看>>
fiddler 不能抓包解决
查看>>
信号完整性概述
查看>>