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\)搜索一下对于每个有用的苹果数量,满 ...
随机推荐
- python调用虹软2.0(全网首发)-更新中
python调用虹软2.0目前没有任何demo可以参考,自己研究了2个晚上终于把第一步做出来了,使用了opencv来加载和显示图片,龟速更新中 这一版作废,新版已发出:https://www.cnbl ...
- 【Python】 简易实现接口测试自动化
实现思路 使用excel管理用例用例信息,requests模块发送http请求,实现了记录日志,邮件发送测试报告的功能 目录结构如下: 下面直接上代码: 统筹脚本 # -*- coding:utf-8 ...
- 自已开发完美的触摸屏网页版仿app弹窗型滚动列表选择器/日期选择器
手机端网页版app在使用下拉列表时,传统的下拉列表使用起来体验非常不好,一般做的稍好一点的交互功能界面都不会直接使用下拉列表,所以app的原生下拉列表都是弹窗列表选择,网页型app从使用体验上来当然也 ...
- slim(4621✨)
用于代码瘦身. 老鸟建议:不要混写js 和 html,如果避免不了,当前文件可以改为erb格式,混用slim和erb不是什么问题. git: https://github.com/slim-temp ...
- html合并单元格
在合并的首位置加上colspan或者rowspan属性即可 code: <html> <body> <h4>横跨两列的单元格:</h4> < ...
- tcpdump 使用实例
详细的文档见tcpdump高级过滤技巧 基本语法 ========过滤主机--------- 抓取所有经过 eth1,目的或源地址是 192.168.1.1 的网络数据# tcpdump -i eth ...
- day30 主机管理-堡垒机3-操作记录
课堂代码:https://github.com/liyongsan/git_class/tree/master/day30
- UML类图几种关系
(转自:http://www.open-open.com/lib/view/open1328059700311.html) 在UML类图中,常见的有以下几种关系: 泛化(Generalization) ...
- [Web UI]对比Angular/jQueryUI/Extjs:没有一个框架是万能的
Angular不能做什么?对比Angular/jQueryUI/Extjs 框架就好比兵器,你得明白你手里拿的是屠龙刀还是倚天剑,刀法主要是砍,剑法主要是刺.对于那些职业喷子和脑残粉,小僧送你们两个字 ...
- 网关-zuul介绍 第一篇 网关解决的问题
基于网关,我们可以实现 限流,认证,监控,路由 等功能,这样说,我们可能觉得很生硬,那我们反过来推一下, 假如没有网关会怎么样?1 :客户端需要访问多个后端服务,则前端得存储多个后端的地址(ip+po ...