SPOJ104 Highways 【矩阵树定理】
SPOJ104 Highways
Description
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.
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
矩阵树定理模板题,有一些细节:
- 发现自由元立即切出去
- 输出答案时向下取整(懵)
然后就没了
#include<bits/stdc++.h>
using namespace std;
#define N 3010
int n,m;
double g[N][N];
void gauss(){
n--;
for(int i=1;i<=n;i++){
int r=i;
for(int j=i+1;j<=n;j++)
if(fabs(g[j][i])>fabs(g[r][i]))r=j;
if(r!=i)for(int j=1;j<=n+1;j++)swap(g[i][j],g[r][j]);
if(!g[i][i]){cout<<0<<endl;return;}
for(int k=i+1;k<=n;k++){
double f=g[k][i]/g[i][i];
for(int j=i;j<=n+1;j++)g[k][j]-=f*g[i][j];
}
}
double ans=1;
for(int i=1;i<=n;i++)ans*=g[i][i];
printf("%.0f\n",abs(ans));//一定要加 向下取整
}
int main(){
int T;cin>>T;
while(T--){
memset(g,0,sizeof(g));
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
g[x][x]++;g[y][y]++;
g[x][y]--;g[y][x]--;
}
gauss();
}
return 0;
}
SPOJ104 Highways 【矩阵树定理】的更多相关文章
- spoj104 HIGH - Highways 矩阵树定理
欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...
- SPOJ Highways [矩阵树定理]
裸题 注意: 1.消元时判断系数为0,退出 2.最后乘ans要用double.... #include <iostream> #include <cstdio> #includ ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- spoj104 highways 生成树计数(矩阵树定理)
https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...
- 【SPOJ】Highways(矩阵树定理)
[SPOJ]Highways(矩阵树定理) 题面 Vjudge 洛谷 题解 矩阵树定理模板题 无向图的矩阵树定理: 对于一条边\((u,v)\),给邻接矩阵上\(G[u][v],G[v][u]\)加一 ...
- 算法复习——矩阵树定理(spoj104)
题目: In some countries building highways takes a lot of time... Maybe that's because there are many p ...
- BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]
传送门 题意: 给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图$K_{n,m}$ 求生成树个数 1 <= n,m,p <= 10^18 显然不能暴力上矩阵树定理 看 ...
- bzoj 4596 [Shoi2016]黑暗前的幻想乡 矩阵树定理+容斥
4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 559 Solved: 325[Submit][Sta ...
- 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)
[LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...
随机推荐
- jmeter-对响应数据进行unicode转码
1,请求接口成功后,返回数据为unicode编码,查看不方便
- linux pgrep命令的使用
pgrep 是通过程序的名字来查询进程的工具,一般是用来判断程序是否正在运行. 常用参数 -l 列出程序名和进程ID -o 进程起始的ID -n 进程终止的ID $ ps -ef | grep mys ...
- 如何优雅地使用 rm 防止误删除?
IT 界的有一个老梗,一次某论坛的数据库管理员抱怨自己老板一直虐待他,结果他一气之下就删库跑路了...... 于是... 据新华社北京 8 月 20 日电 ,北京一软件工程师徐某离职后因公司未能如期结 ...
- js执行环境的周边概念
一.熟悉几个名词: 1.执行环境(execution context),也叫执行上下文,每个函数都会有自己的执行环境:当浏览器首次加载脚本时,他将默认进入全局执行环境:如果接下来要调用一个内部函数,则 ...
- [java]Stream API——map和flatMap的区别
map方法 map的含义为映射.是指对于Stream中包含的每一个元素使用给定的转换函数进行转换. map传入的Lambda表达式必须是Function实例,参数可以为任意类型,而其返回值也是任性类型 ...
- ADC和RTC的寄存器的读取
ADC的寄存器读取,int adc_read(void){ int result; #if ADSTART==0 result = ADC.ADCDAT0&0x3ff; while(!(ADC ...
- C# SQLite写入和读取DateTime类型
很简单 1.不要相信网上大部分人说的话,比如存到int里 (ps:版本差距知道吗?) 2.nuget包下载最新版的sqlite 3.SQLite支持DateTime类型(图形化工具不会给提示无视它), ...
- How to find per-process I/O statistics on Linux
以下转自http://www.xaprb.com/blog/2009/08/23/how-to-find-per-process-io-statistics-on-linux/ Newer Linux ...
- Maven 中的dependencies与dependencyManagement的区别
1.dependencyManagement 在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器 在pom.xml文件中,jar的版本判断的 ...
- hdu4678 Mine 规律或者博弈。(博弈的sg函数不懂我是找的规律)
链接:题意就是告诉你一个扫雷图里面每个雷的位置,有两个人,每个人都知道雷的确切位置,每个人一次可以点一部,问你谁能赢. 链接:http://acm.hdu.edu.cn/showproblem.php ...